NSException kills the app if raised in background

蹲街弑〆低调 提交于 2019-12-11 20:02:38

问题


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?

UPDATE

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. 

These functions are in MailCore Library. Now if someone knows the solution please help me.


回答1:


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



来源:https://stackoverflow.com/questions/9481457/nsexception-kills-the-app-if-raised-in-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!