2010-01-13 164 views
2

如何測試NSURLConnection的失敗?測試NSURLConnection失敗

此外,您如何判斷是否由於飛行模式或WiFi關閉而失敗?在我的測試中,當警報彈出告訴用戶他們需要打開WiFi時,如果他們忽略它,我的應用程序就坐在那裏並等待響應。

回答

4

如果連接的代表是自我,你可以像這樣:

self.myConnection = [[[NSURLConnection alloc] initWithRequest:urlRequest delegate:self] autorelease]; 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
    if ([error code] == kCFURLErrorNotConnectedToInternet) 
    { 
     // if we can identify the error, we can present a more precise message to the user. 
     NSDictionary *userInfoDict = [NSDictionary dictionaryWithObject:@"No Connection Error" 
                  forKey:NSLocalizedDescriptionKey]; 
     NSError *noConnectionError = [NSError errorWithDomain:NSCocoaErrorDomain 
                 code:kCFURLErrorNotConnectedToInternet 
                userInfo:userInfoDict]; 
     [self handleError:noConnectionError]; 
    } 
    else 
    { 
     // otherwise handle the error generically 
     [self handleError:error]; 
    } 
} 
+0

這裏是什麼的HandleError – 2014-02-07 10:23:23

+0

這是你可以創建處理錯誤的方法的示例名稱。 – 2014-02-07 18:44:06