Theory – Easy Reachability for iPhone / iPod Touch Webview Development

photics-reachability-thumbnailAhh… my article starts out with a hedge… a single word that could negate the value of this tutorial… theory! Apple has built a walled garden. It is their happy little nirvana, filled with thousands of apps and billions of downloads. Your apps are invited to this party, if you can get past the checklist. If you have an online app, there is one pesky requirement that could instantly thwart your dreams – reachability. Your app needs to test for Internet connectivity. If it doesn’t, it risks rejection!

I’ve read the online tutorials. I’ve searched the web. Bing, Google, Apple’s documentation… nothing made sense. I’m more familiar with web programming. I’m not used to this Objective-C stuff. Everything out there seemed needlessly complicated. My goal was simple… if Photics.com could not be reached, give the user an error. Apple is very picky about the user interface. They want to create a pleasant experience for their customers. I can’t say that I blame them. Their requirement makes a lot of sense. But the tutorials that I’ve read… I couldn’t make sense out of it.

That’s when I started to wonder about the Webview. My apps are based on displaying web content. Couldn’t I simply do this with HTML or Javascript? I went back to the search engines to research the matter. Sure enough, it could be done. With one simple line of code, I could do basic Reachability checking…

alert-example

1
2
3
<img src="http://photics.com/games/conquest/images/login.jpg"
onError="alert('Photics.com is not available. Check your Internet
connection and try again.')">

OK, so that’s three lines… but that’s just for readability of my reachability code ;-)

The trick is with the “onError” command. If the image cannot be loaded, display an error alert. If you’re familiar with web development, you might be twitching right about now. “But… but… onError is not supported by all browser.” It doesn’t matter. The iPhone OS does support it! That’s the beauty of iPhone development. There’s only one browser to support. I admit it, this is not an elegant fix. Yet, there are advantages.

The Good

  • It’s easy. Only one line of HTML and one image required.
  • It does care if you’re using WiFi or 3G.
  • You don’t need the Reachability.h or Reachability.m files in your app.
  • It doesn’t care if it’s OS version 2.2.1 or 3.1

The Bad

  • It doesn’t give the user an option. It’s only a warning.
  • It’s annoying. I hate pop-up alerts.
  • Apple may not approve it.

That last point is the real issue. It’s the reason why the article title starts with the word “Theory”. While Apple quickly approved my first app. My second app was rejected. I’m learning how fickle they can be. While my HTML / Javascript Reachability trick works, I’m still wondering if my app will be rejected. Although, I’ve been getting that doubtful feeling with all of my apps. I should know in a few more days (weeks?) if my idea worked.

4 thoughts on “Theory – Easy Reachability for iPhone / iPod Touch Webview Development”

  1. Yes, the app with this version of Reachability code was approved. Although, there is another way to do it. I started with a View-Based application. The Reachability code is automatically included. It just needs to be un-commented.

  2. Hallo, I have been trying this solution but somehow I can’t get it to work. So please excuse my maybe stupid question, where do you put this code. I have it inside the body tag and I am using the standard phonegap.js.
    Thanks for helping.

  3. Hello simsim,

    I put the image code in the body of the HTML page, stored locally in the app, and I put the image file on my web server. So, if the file doesn’t load, it causes the error to be displayed.

    Although, I’m not sure if I would use this method anymore. Last I checked, Apple included Reachability code automatically with View-Based applications. It just has to be un-commented. That’s a more elegant way to do reachability checking.

Comments are closed.