Yazan alhoroub
Yazan alhoroub

Reputation: 110

What is wrong with this method of checking for an internet connection?

While looking for a simple asynchronous method to check for internet access in iOS, I had an idea, using a UIWebView to handle asynchronicity:

-(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
}

This seems to be working, but I wanted to know if there are any hidden dangers I might not be aware of in this approach. Any ideas?

Upvotes: 2

Views: 88

Answers (1)

Linuxios
Linuxios

Reputation: 35783

There are two drawbacks to this approach, but there's not much to be done:

  1. Data Use: On a device using cellular data, you are using data. If the file is small enough it shouldn't be noticible, but it's still there.
  2. If you are checking for Internet access in general, and not just for that site, if that site is down, your app will assume that the Internet is down. This can be mitigated by running the check service on high availability cloud servers, or by using very high availability websites.

Upvotes: 2

Related Questions