UIAlertController аварийно завершает работу приложения при представлении из NSURLSessionDataTask? - PullRequest
0 голосов
/ 18 марта 2020

Почему это не работает?

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 внутри блока вызвала это. Я использую последние версии Xcode и SDK, ориентируясь на iOS 8+. Спасибо!

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

1 Ответ

0 голосов
/ 18 марта 2020

Какое сообщение об ошибке вы получаете при сбое? Я предполагаю, что вы пытаетесь представить предупреждение из фонового потока, и в этом случае вам нужно представить его так:

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