如何测试NSURLConnection的失败?
另外,如何判断它是否因飞机模式或WiFi关闭而失败?在我的测试中,当警报弹出告诉用户他们需要打开WiFi时,如果他们忽略它,我的应用程序就坐在那里并旋转等待响应。
答案 0 :(得分: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];
}
}