2010-02-14 63 views
0

很新的網絡編程和特別客觀的c網絡...
即使我關閉了計算機上的網絡接口,或者將我的iPhone設置爲風險模式, NSURLConnection不會 返回零與應用程序死亡的不愉快的驚喜。即使網絡出現故障,nsurlconnecion也不返回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]; 
} 

回答

2

您的應用程序可能會崩潰,因爲你釋放連接對象,即使你沒有擁有它。從委託方法中刪除[connection release];

+0

...和諾貝爾獎&&#&#「#%&去找我... Thanx! – niel41 2010-02-14 19:30:30