如果在后台引发NSException,则会杀死该应用 [英] NSException kills the app if raised in background
问题描述
我正在执行一些与网络相关的代码.当代码开始执行并且用户在后台发送应用程序时,如果出现异常,则该应用程序将被终止.如果未引发异常,则不会终止应用程序.是否可以停止/暂停任何类型的网络活动?或阻止应用程序终止的方法?
I am performing some network related code. When the code starts executing and the user sends the app in background and that time if exception raise the app is killed. if exception is not raised app is not killed. Is it possible to stop/pause any kind of network activity? or a way to stop app from killing?
更新
我正在使用哑光加拉哈尔(充满爱的可可)书写的未捕获异常处理程序.触发请求后,如何取消该请求.请求代码在此函数中调用
I am using matt gallahar (cocoa with love) written uncaughtexception handler. How do I cancel the request once it fired. The code for request is called in this function
+(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;
}
如果我输入了不同的端口号,那么在下一行出现异常时便会发生
The Exception occurs If I enter different Port number that time the exception raises in the following line
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库中.现在,如果有人知道解决方案,请帮助我.
These functions are in MailCore Library. Now if someone knows the solution please help me.
推荐答案
您是否为应用程序注册了uncaughtExceptionHandler?另外,Apple确实建议您在关闭(使用- (void)applicationDidEnterBackground:(UIApplication *)application
)之前取消所有打开的网络请求(或至少准备使它们失败).请参阅此链接的成为负责任的后台应用程序"部分:
Have you registered an uncaughtExceptionHandler for your application? Also, Apple does recommend that you cancel any open network requests (or at least be prepared for them to fail) prior to shutting down (use - (void)applicationDidEnterBackground:(UIApplication *)application
). See the "Being a Responsible Background Application" section of this link: https://developer.apple.com/library/ios/DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW47
这篇关于如果在后台引发NSException,则会杀死该应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!