This post answers the following questions:
- How can I check if the device is connected to the Internet?
- What do I do to make sure my app doesn’t crash when I access Internet resources?
If your application uses the Internet, but crashes if the user is not connected, Apple may reject your app upon App Store submission. It’s therefore your responsibility to check to make sure Internet resources are available before you attempt to use them. Unfortunately, there’s not a simple application variable or function to accomplish this check.
There are many ways of doing this out there, and our method is a little bit different. It leverages the tools we learned in the previous 3 parts to this tutorial, presents the user with a clean, “Checking for Internet…” message, and leaves us with a more stable app.
Create a UIWebView as we did in the past three posts, and then add a boolean member variable and two more functions — in your .h:
BOOL internetOK;
...
- (void)tryInternet;
- (void)verifyInternet;
…and your .m:
- (void)tryInternet {
internetOK = NO;
[wWw stringByEvaluatingJavaScriptFromString:@"window.location='http://www.fourtentech.com/';"];
}
- (void)verifyInternet {
if(!internetOK) {
// no Internet access -- display error or take other action
}
}
We’ll also need to include a new UIWebViewDelegate method:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
internetOK = YES;
}
What we’ve got here is a “check” function that sets our “Internet is OK” flag to NO and then tries to load a page. If the page loads, webViewDidFinishLoad: is called and our flag is set to YES. After a certain number of seconds (a timeout), we’ll call verifyInternet to see if our flag is set to YES, and if not, we know there’s been a failure.
To make this happen, present the user with a UIWebView with a message that you’re checking for Internet access (you can also do this with a hidden UIWebView and do it in the background!). Here’s one we used in our own iPhone app:
[wWw loadHTMLString:@"<body bgcolor='#DDDDDD'><font face='Helvetica'><br /><br /><br /><br /><br /><font color='#3080A0'><center><b>Connecting to FourTen's server...</b><center></font></font></body>" baseURL:nil];
Then set our two functions to run:
[self performSelector:@selector(tryInternet) withObject:nil afterDelay:1.0];
[self performSelector:@selector(verifyInternet) withObject:nil afterDelay:15.0];
The first one we run after 1 second, because if you try to run it simultaneously with loading the HTML string, it won’t work. The second one we run after 15 seconds, making our timeout 14 seconds. You can choose to wait more or less time. After calling the first, internetOK will only be YES if Internet is detected.
This is the end of a four-part series on the UIWebView — we hope it’s been useful!
FourTen Technologies, Inc., is a leading US iPhone app development firm. For information on having FourTen build a custom mobile application for your company, visit www.fourtentech.com. Article written by Jonathan Corbett (President & CEO, FourTen). Contact: jcorbett@fourtentech.com.




