即使网络关闭,nsurlconnecion也不会返回nil

时间:2010-02-14 12:38:53

标签: iphone nsurlconnection

网络编程和特殊客观网络的新手...... 即使我关闭了计算机上的网络接口,或者将我的iPhone设置为轻薄模式,NSURLConnection也不会 应用程序死亡时,令人不快的意外返回nil。

我使用所有正确的回调方法符合NSURLConnection协议......

那么你如何处理客户端没有互联网连接的情况呢?

我将登录数据发布到服务器的代码段:

-(void)Login {
     ...
     NSURLConnection *theConnection = [NSURLConnection connectionWithRequest:post delegate:self];
    if (theConnection==nil) {
         //Here I want to show an alert but this case never happens....
    }
    else {
         receivedData=[[NSMutableData data] retain];
    }
}
...


//This callback is called correctly but no alert shows up and after this method app dies.

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    [connection release];
    [receivedData release];
    [self setLoggedIn:NO];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                              message:[error localizedDescription]
                                              delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles: nil];
    [alert show];
    [alert release];
}

1 个答案:

答案 0 :(得分:2)

您的应用可能会崩溃,因为即使您不拥有连接对象也是如此。从委托方法中删除[connection release];