2012-02-28 75 views
0

我正在執行一些與網絡相關的代碼。當代碼開始執行並且用戶在後臺發送應用程序時,如果異常提示應用程序被終止。如果沒有提出異常,應用程序不會被殺死。是否有可能停止/暫停任何類型的網絡活動?或者阻止應用程序被殺的方法?如果在後臺引發NSException會拋出應用程序

UPDATE

我使用書面uncaughtexception處理亞光gallahar(可可愛)。一旦它被解僱,我該如何取消這個請求。對於要求輸入密碼在此功能

+(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

什麼是異常名稱? – joerick 2012-02-28 11:59:17

+0

@joerick查看更新後的問題 – 2012-02-29 04:15:09

回答

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

+0

閱讀更新後的問題請 – 2012-02-29 07:37:28

+0

好了,現在問題已經改變 - 一個不同的方法?爲什麼不從你的'connectToServer'函數返回錯誤代碼int?調用者可以愉快地返回錯誤字符串而不需要提高(並且可能這會被允許在'[application beginBackgroundTaskWithExpirationHandler:]'調用中)繼續。 – 2012-02-29 13:25:16

+0

我無法更新更新整個庫。這些函數是用郵件核心庫編寫的。 – 2012-05-07 06:34:38

相關問題