测试NSURLConnection失败 [英] Test NSURLConnection failure

查看:94
本文介绍了测试NSURLConnection失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何测试NSURLConnection的失败?

How can you test a failure of NSURLConnection?

此外,您如何判断是否由于飞行模式或WiFi关闭而失败?在我的测试中,当弹出警报提示用户他们需要打开WiFi时,如果他们忽略了它,我的应用程序就坐在那儿旋转并等待响应.

Also, how do you tell if it failed due to airplane mode or WiFi being turned off? In my tests, while the alert pops up telling the user they need to turn on WiFi, if they ignore it my App just sits there and spins waiting for a response.

推荐答案

如果连接的委托是self,则可以这样:

If your connection's delegate is self, you can something like this:

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];
    }
}

这篇关于测试NSURLConnection失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆