应用程序在ios中崩溃sendSynchronousRequest

应用程序在ios中崩溃sendSynchronousRequest,ios,Ios,我不知道为什么我的应用程序在这一行崩溃 NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 这是我的代码: NSString * urlString = [[NSUserDefaults standardUserDefaults] objectForKey:MAINURL]; NSHTTPURLResponse *

我不知道为什么我的应用程序在这一行崩溃

NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
这是我的代码:

NSString * urlString = [[NSUserDefaults standardUserDefaults] objectForKey:MAINURL];

NSHTTPURLResponse * response;
NSError * error;
NSMutableURLRequest *request;

request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]
                                    cachePolicy:NSURLRequestReloadIgnoringCacheData
                                   timeoutInterval:120];

if([[[NSUserDefaults standardUserDefaults] objectForKey:@"callWebServiceFor"] isEqualToString:nil]) {
    // [request setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"];

    NSString * strSessionId = [[NSUserDefaults standardUserDefaults] objectForKey:@"sessionId"];
    if(strSessionId != nil)
        [request setValue:strSessionId forHTTPHeaderField:@"Cookie"];
}
NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
// Storing session headers when user logs in successfully
if([[[NSUserDefaults standardUserDefaults] objectForKey:@"callWebServiceFor"] isEqualToString:@"login"]) {
    NSArray * all = [NSHTTPCookie cookiesWithResponseHeaderFields:[response allHeaderFields] forURL:[NSURL URLWithString:urlString]]; //testing
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:all forURL:[NSURL URLWithString:urlString] mainDocumentURL:nil];
    NSString * strSessionId;
    for (NSHTTPCookie *cookie in all) {
        strSessionId = [[NSString alloc] initWithString:cookie.value];
        //  NSLog(@"Session value: %@",cookie.key];
        //  NSLog(@"Session value: %@",strSessionId);
    }
    [[NSUserDefaults standardUserDefaults] setObject:strSessionId forKey:@"sessionId"];
    [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"callWebServiceFor"];
}
if (data != nil) {
    dispatch_async(kBackGroudQueue, ^{
        [self performSelectorOnMainThread:@selector(handleWebServiceResponse:)
                               withObject:data waitUntilDone:YES];
    });
} else {
    [SwapweekUtilViewController showAlert:@"Internet" :@"Check your Internet Connection"];
}

我看不出为什么您的代码在NSURLConnection sendSynchronousRequest:中崩溃,并且您尚未发布stacktrace来验证这一事实,但是如果strSessionId为nil,此代码可能会崩溃:

此代码将崩溃:

使用removeObjectForKey:从用户默认值中删除对象

这也是非常糟糕的:

if([[[NSUserDefaults standardUserDefaults] objectForKey:@"callWebServiceFor"] isEqualToString:nil]) {
测试[NSString length]==0

这也很糟糕:

for (NSHTTPCookie *cookie in all) {
    strSessionId = [[NSString alloc] initWithString:cookie.value];
    //  NSLog(@"Session value: %@",cookie.key];
    //  NSLog(@"Session value: %@",strSessionId);
}

一旦你有一个非空的strSessionId,只需执行strSessionId=cookie.key,就可以中断;而不是initWithString。

即使它在stacktrace后也不会抛出任何错误异常。
if([[[NSUserDefaults standardUserDefaults] objectForKey:@"callWebServiceFor"] isEqualToString:nil]) {
for (NSHTTPCookie *cookie in all) {
    strSessionId = [[NSString alloc] initWithString:cookie.value];
    //  NSLog(@"Session value: %@",cookie.key];
    //  NSLog(@"Session value: %@",strSessionId);
}