【问题标题】:NSException kills the app if raised in background如果在后台引发 NSException 会杀死应用程序
【发布时间】:2012-02-29 12:14:52
【问题描述】:

我正在执行一些与网络相关的代码。当代码开始执行并且用户在后台发送应用程序时,如果出现异常,应用程序就会被终止。如果未引发异常,则不会杀死应用程序。是否可以停止/暂停任何类型的网络活动?或阻止应用程序被杀的方法?

更新

我正在使用 matt gallahar (cocoa with love) 编写的未捕获异常处理程序。触发后如何取消请求。请求的代码在这个函数中被调用

+(NSString*)validate:(NSString*)username password:(NSString*)password server:(NSString*)server port:(int)port encryption:(int)encryption authentication:(int)authentication folders:(NSMutableArray*)folders {
    CTCoreAccount* account = [[CTCoreAccount alloc] init];

    @try   {
        [account connectToServer:server port:port connectionType:encryption authType:authentication login:username password:password];
    } @catch (NSException *exp) {
        NSLog(@"connect exception: %@", exp);
        return [NSString stringWithFormat:@"Connect error: %@", [ImapFolderWorker decodeError:exp]]; 
    }
}


- (void)connectToServer:(NSString *)server port:(int)port 
        connectionType:(int)conType authType:(int)authType
        login:(NSString *)login password:(NSString *)password {
    int err = 0;
    int imap_cached = 0;

    const char* auth_type_to_pass = NULL;
    if(authType == IMAP_AUTH_TYPE_SASL_CRAM_MD5) {
        auth_type_to_pass = "CRAM-MD5";
    }

    err = imap_mailstorage_init_sasl(myStorage,
                                     (char *)[server cStringUsingEncoding:NSUTF8StringEncoding],
                                     (uint16_t)port, NULL,
                                     conType,
                                     auth_type_to_pass,
                                     NULL,
                                     NULL, NULL,
                                     (char *)[login cStringUsingEncoding:NSUTF8StringEncoding], (char *)[login cStringUsingEncoding:NSUTF8StringEncoding],
                                     (char *)[password cStringUsingEncoding:NSUTF8StringEncoding], NULL,
                                     imap_cached, NULL);

    if (err != MAIL_NO_ERROR) {
        NSException *exception = [NSException
                exceptionWithName:CTMemoryError
                reason:CTMemoryErrorDesc
                userInfo:nil];
        [exception raise];
    }

    err = mailstorage_connect(myStorage);
    if (err == MAIL_ERROR_LOGIN) {
        NSException *exception = [NSException
                exceptionWithName:CTLoginError
                reason:CTLoginErrorDesc
                userInfo:nil];
        [exception raise];
    }
    else if (err != MAIL_NO_ERROR) {
        NSException *exception = [NSException
                exceptionWithName:CTUnknownError
                reason:[NSString stringWithFormat:@"Error number: %d",err]
                userInfo:nil];
        [exception raise];
    }
    else    
        connected = YES;
}

如果我在以下行中输入不同的端口号,则会出现异常

NSException *exception = [NSException
                    exceptionWithName:CTUnknownError
                    reason:[NSString stringWithFormat:@"Error number: %d",err]
                    userInfo:nil];
            [exception raise]; // this line kills the app if the app is in background. 

这些函数在 MailCore 库中。现在,如果有人知道解决方案,请帮助我。

【问题讨论】:

  • @joerick 查看更新的问题

标签: ios network-protocols background-process


【解决方案1】:

您是否为您的应用程序注册了一个 uncaughtExceptionHandler?此外,Apple 确实建议您在关闭之前取消任何打开的网络请求(或至少为它们失败做好准备)(使用- (void)applicationDidEnterBackground:(UIApplication *)application)。请参阅此链接的“成为负责任的后台应用程序”部分:https://developer.apple.com/library/ios/DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW47

【讨论】:

  • 好的,现在问题已经改变了 - 不同的策略?为什么不从 connectToServer 函数返回错误代码 int ?调用者可以很高兴地返回错误字符串而无需引发(并且可能即使在 [application beginBackgroundTaskWithExpirationHandler:] 调用中也可以继续执行此操作)。
  • 我无法更新更新整个库。函数写在邮件核心库中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-15
  • 1970-01-01
  • 1970-01-01
  • 2013-02-04
  • 1970-01-01
  • 2020-10-23
相关资源
最近更新 更多