2

在寻找一种简单的异步方法来检查 iOS 中的 Internet 访问时,我有一个想法,使用 UIWebView 来处理异步:

-(void) testNet
{
    UIWebView *webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    webview.delegate=self;
    NSString *urlAddress = @"http://bla.com/dummySmallFile.txt";
    NSURL *url = [NSURL URLWithString:urlAddress];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
   [webview loadRequest:requestObj];
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    //deal with no net stuff here
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    //net is there, use it here
}

这似乎有效,但我想知道在这种方法中是否存在我可能没有意识到的任何隐患。有任何想法吗?

4

1 回答 1

2

这种方法有两个缺点,但没有什么可做的:

  1. 数据使用:在使用蜂窝数据的设备上,您正在使用数据。如果文件足够小,它不应该被注意到,但它仍然存在。
  2. 如果您正在检查一般的 Internet 访问,而不仅仅是针对该站点,如果该站点已关闭,您的应用程序将假定 Internet 已关闭。这可以通过在高可用性云服务器上运行检查服务或使用非常高可用性的网站来缓解。
于 2013-09-30T21:51:34.050 回答