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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UIAlertController从NSURLSessionDataTask出现时使应用程序崩溃?相关的知识,希望对你有一定的参考价值。

为什么这不起作用?

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];

以下行使我的应用程序崩溃,似乎试图在块内展示我的UIAlertController导致了此情况。我针对ios 8+使用最新的Xcode和SDK。谢谢!

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

您在崩溃时收到的错误消息是什么?我的猜测是您正在尝试从后台线程显示警报,在这种情况下,您需要像这样显示警报:

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

以上是关于UIAlertController从NSURLSessionDataTask出现时使应用程序崩溃?的主要内容,如果未能解决你的问题,请参考以下文章

从 MSMessagesAppViewController 正确显示 UIAlertController

如何从 Appdelegate 显示 UIAlertController

从 UIAlertController 按钮单击导航到 ViewController

如何从 UIAlertcontroller 关闭模态 ViewController

从 UIAlertView 到 UIAlertController

我可以从 IBAction 中调用 UIAlertController [重复]