简体   繁体   English

从 NSURLSessionDataTask 呈现时 UIAlertController 使应用程序崩溃?

[英]UIAlertController crashes app when presented from NSURLSessionDataTask?

Why does this not work?为什么这不起作用?

NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:theRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
    NSString *receivedDataString = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
    NSString *keyInfo = [[[[NSBundle mainBundle] infoDictionary] objectForKey:keyString] stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
    NSLog(@"%@", receivedDataString);
    NSLog(@"%@", keyInfo);

    if (![receivedDataString isEqual:keyInfo] && ![receivedDataString isEqual: @""])
    {
        NSFileManager *fileManager = [NSFileManager defaultManager];

        UIAlertController *alertCheckForUpdate;

        UIAlertAction *noUpdateBtn = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}];
        UIAlertAction *yesCydiaUpdateBtn = [UIAlertAction actionWithTitle:@"Cydia" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"cydia://sources"] options:@{} completionHandler:nil];
        }];
        UIAlertAction *yesSileoUpdateBtn = [UIAlertAction actionWithTitle:@"Sileo" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"sileo://package/com.dave1482.powerapp"] options:@{} completionHandler:nil];
        }];

        if ([fileManager fileExistsAtPath:sileoPath] && ![fileManager fileExistsAtPath:cydiaPath]){
            alertCheckForUpdate = [UIAlertController alertControllerWithTitle:@"New Update Available" message:[NSString stringWithFormat:@"Open Sileo to update PowerApp to v%@?", receivedDataString] preferredStyle:UIAlertControllerStyleAlert];
            [alertCheckForUpdate addAction:yesSileoUpdateBtn];
        } else if (![fileManager fileExistsAtPath:sileoPath] && [fileManager fileExistsAtPath:cydiaPath]) {
            alertCheckForUpdate = [UIAlertController alertControllerWithTitle:@"New Update Available" message:[NSString stringWithFormat:@"Open Cydia to update PowerApp to v%@?", receivedDataString] preferredStyle:UIAlertControllerStyleAlert];
            [alertCheckForUpdate addAction:yesCydiaUpdateBtn];
        } else if ([fileManager fileExistsAtPath:sileoPath] && [fileManager fileExistsAtPath:cydiaPath]) {
            alertCheckForUpdate = [UIAlertController alertControllerWithTitle:@"New Update Available" message:[NSString stringWithFormat:@"Open Cydia/Sileo to update PowerApp to v%@?", receivedDataString] preferredStyle:UIAlertControllerStyleAlert];
            [alertCheckForUpdate addAction:yesCydiaUpdateBtn];
            [alertCheckForUpdate addAction:yesSileoUpdateBtn];
        } else {
            alertCheckForUpdate = [UIAlertController alertControllerWithTitle:@"New Update Available" message:[NSString stringWithFormat:@"PowerApp v%@ is available but for some reason you don't have Sileo or Cydia.", receivedDataString] preferredStyle:UIAlertControllerStyleAlert];
        }
        [alertCheckForUpdate addAction:noUpdateBtn];
        [self presentViewController:alertCheckForUpdate animated:YES completion:nil];
    }
}];
[dataTask resume];

The following line crashes my app, it seems attempting to present my UIAlertController inside the block caused this.以下行使我的应用程序崩溃,似乎试图在块内呈现我的 UIAlertController 导致了这个。 I am using the latest Xcode and SDK while targeting iOS 8+.我正在使用最新的 Xcode 和 SDK,同时针对 iOS 8+。 Thank you!谢谢!

[self presentViewController:alertCheckForUpdate animated:YES completion:nil];

What is the error message you're getting upon crashing?您在崩溃时收到的错误消息是什么? My guess is you're trying to present the alert from a background thread in which case you'll need to present it like so:我的猜测是您正在尝试从后台线程呈现警报,在这种情况下,您需要像这样呈现它:

dispatch_async(dispatch_get_main_queue(), ^{
    [self presentViewController:alertCheckForUpdate animated:YES completion:nil];
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 出现键盘时,应用程序在模拟器中崩溃 - App crashes in simulator when ever keyboard is presented 解散显示的UIViewController时,应用偶尔崩溃 - App crashes sporadically when dismissing a presented UIViewController 当UIAlertcontroller在Swift中出现时,KEEP键盘是否打开? - KEEP keyboard ON when UIAlertcontroller is presented in Swift? 尝试更改UIAlertController的消息颜色时,应用程序崩溃 - app crashes when trying to change Message color of UIAlertController,swift 出现UIActionSheet时,应用程序频繁崩溃 - App crashes frequently at time when UIActionSheet would be presented UIActivityViewController 在设备上显示时崩溃 - UIActivityViewController crashes when presented on device NSURLSessionDataTask:NSOperationQueue中的应用程序崩溃 - NSURLSessionDataTask: Application crashes in NSOperationQueue 当呈现UIAlertController时,UIBarButtonItem丢失自定义外观属性 - UIBarButtonItem loses custom appearance attributes when UIAlertController is presented 尝试显示带有消息的UIAlertController时,应用程序崩溃:由于未捕获的异常'NSInvalidArgumentException',终止了应用程序 - App crashes when attempting to display UIAlertController with message: Terminating app due to uncaught exception 'NSInvalidArgumentException' 在“可见”视图上,仅允许从类中呈现一个UIAlertController视图 - Allow only one UIAlertController View to be presented from a Class on the Visible view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM