【发布时间】: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