UIAlertController 自 iOS 13 后消失

UIAlertController disappearing since iOS 13

我有以下弹出 UIAlert 的功能,允许用户更新他们的触觉反馈设置:

- (void)requestHapticSetting{
    UIWindow *alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    alertWindow.rootViewController = [[UIViewController alloc] init];
    alertWindow.windowLevel = UIWindowLevelAlert + 1;
    [alertWindow makeKeyAndVisible];

    if(isHapticOn){
        hapticMessage = @"Haptic feedback is currently\nturned ON.\nPlease update preference.";
    }
    else {
        hapticMessage = @"Haptic feedback is currently\nturned OFF.\nPlease update preference.";
    }

    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Haptic Setting"
                                                                   message:hapticMessage
                                                            preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction* onAction = [UIAlertAction actionWithTitle:@"TURN ON" style:UIAlertActionStyleDefault
                                                     handler:^(UIAlertAction * action) {
                                                         [self saveHapticSettingOn];
                                                     }];

    UIAlertAction* offAction = [UIAlertAction actionWithTitle:@"TURN OFF" style:UIAlertActionStyleDefault
                                                      handler:^(UIAlertAction * action) {
                                                          [self saveHapticSettingOff];
                                                      }];
    [alert addAction:offAction];
    [alert addAction:onAction];
    [alertWindow.rootViewController presentViewController:alert animated:YES completion:nil];
}

我已经用了几年了,效果很好。

但是,自从更新到 iOS 13 并更新到最新的 Xcode,我的警报弹出不到一秒钟就关闭了。

是什么改变了这一切?提前致谢。

似乎发生了变化的是,在 iOS 12 和之前的版本中,您的应用程序将通过在 iOS 中调用 [alertWindow makeKeyAndVisible]; 来强烈引用 window ] 13 现在没有了。

发生的事情是,对您的 alertWindow 的唯一强引用是在您的 requestHapticSetting 函数中,一旦这个函数 returns,window 就被销毁了,从而从视图中删除您的提醒。

这可能仅通过采用 iOS 13 个场景就可以解决,但我还没有测试过。我可以建议的是,如果您使用场景 , 将无法正常工作,请将您的警报 window 保存在代码中某处的强变量中,然后使用它来提出警报。我建议在单例或 AppDelegate 本身中这样做。

//AppDelegate.h
...
@property (strong) UIWindow *alertWindow;
....

//AppDelegate.m
...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    ...
    self.alertWindow = [[UIWindow alloc] init];
    self.alertWindow.rootViewController = [[UIViewController alloc] init];
    self.alertWindow.windowLevel = UIWindowLevelAlert + 1;
    ...
}
...

//Your class that's presenting the alert
#import "AppDelegate.h"
...
- (void)requestHapticSetting{
    AppDelegate *appDelegate = (AppDelegate *)UIApplication.sharedApplication;
    [appDelegate.alertWindow makeKeyAndVisible];
    if(isHapticOn){
        hapticMessage = @"Haptic feedback is currently\nturned ON.\nPlease update preference.";
    } else {
        hapticMessage = @"Haptic feedback is currently\nturned OFF.\nPlease update preference.";
    }

    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Haptic Setting"
                                                               message:hapticMessage
                                                        preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction* onAction = [UIAlertAction actionWithTitle:@"TURN ON" style:UIAlertActionStyleDefault
                                                 handler:^(UIAlertAction * action) {
                                                      [self saveHapticSettingOn];
                                                      [appDelegate.alertWindow setHidden:YES];
                                                 }];

    UIAlertAction* offAction = [UIAlertAction actionWithTitle:@"TURN OFF" style:UIAlertActionStyleDefault
                                                  handler:^(UIAlertAction * action) {
                                                      [self saveHapticSettingOff];
                                                      [appDelegate.alertWindow setHidden:YES];
                                                  }];
    [alert addAction:offAction];
    [alert addAction:onAction];
    [appDelegate.alertWindow.rootViewController presentViewController:alert animated:YES completion:nil];
}

对于 Swift 代码,检查

我创建了一个助手 class,它支持新的 UIWindowScene 和 iOS 13.X 和 swift 5.X。大家可以试试

https://github.com/emraz/ERAlertController