我正在执行一些与网络相关的代码。当代码开始执行并且用户在后台发送应用程序时,如果异常引发则应用程序被杀死。如果没有引发异常应用程序未被杀死。是否可以停止/暂停任何类型的网络活动?还是一种阻止应用程序被杀的方法?
更新
我正在使用matt gallahar(cocoa with love)写的uncaughtexception处理程序。如何解除请求后如何取消请求。请求代码在此函数中调用
+(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库中。现在,如果有人知道解决方案,请帮助我。
答案 0 :(得分:0)
您是否为您的应用程序注册了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