39

I'm working on an iPhone application that makes a few calls to web services. I posted this application on the Apple store but it got rejected (and rightly so) since there was no error message displayed to the user if no Internet connection is available. Since obviously the application would not work without it.

So I just wanted to know how to best achieve this? I'm guessing something needs to go in the viewDidLoad method that will throw an alert box saying something like "You need an Internet connection to use this application".

Any ideas would be appreciated.

Cœur
  • 37,241
  • 25
  • 195
  • 267
givp
  • 2,494
  • 6
  • 31
  • 30

3 Answers3

48

If your application must have network access the easiest way is to add the following settings to your info.plist as boolean values.

  • SBUsesNetwork - Ensure the device has an active connection (Edit: not applicable, this seems to be a private API someone found at some point. It is not in Apple's developer documentation.)
  • UIRequiresPersistentWiFi - Ensures the device is connected via WiFi

If your choice is not true then the user will be presented with an appropriate message when starting your application. Best of all this message is from the OS and thus is localized.

If your application cannot download data from a website while running (loss of signal, site down) you should still warn the user though and not just spin indefinitely.

occulus
  • 16,959
  • 6
  • 53
  • 76
Andrew Grant
  • 58,260
  • 22
  • 130
  • 143
  • 1
    Do you have to assign a value to "SBUsesNetwork" or just add it as a key? I can't find any documentation on it in the iPhone SDK or the Apple Developer Support site :-( – Teflon Ted Mar 26 '09 at 00:30
  • They're both boolean's so should be set to true – Andrew Grant Mar 30 '09 at 21:44
  • 7
    Correct me if I'm wrong, but these settings seem to have limitations. SBUsesNetwork only causes an alert if the phone is in airplane mode. UIRequiresPersistentWiFi only causes an alert if WiFi is enabled but not connected. Neither setting seems to cause an alert if user has disabled WiFi. – clint Apr 06 '09 at 15:12
  • Seems that these flags aren't enough anymore. My app just got rejected with them set. I'll have to take a look at the Reachability demo. – brianegge Oct 14 '09 at 20:36
  • 1
    Are you sure SBUsesNetwork isn't deprecated or something? I find it hard to believe that to allow an app on App Store, Apple requires the setting of a flag which they don't mention even once anywhere in their documentation... – Kuba Suder Oct 30 '09 at 16:37
  • I'm not aware of it's current status, but they don't "require" this flag to be set. It's an optional flag for apps that do not have any form of offline mode. The same way there's a flag for apps that only support landscape mode. – Andrew Grant Nov 06 '09 at 18:21
  • Nice tips. Instead of setting in plist, is there any way to change the values dynamically? – Raptor Dec 12 '09 at 03:09
  • @brianegge If you have fixed your app, can you post on how exactly you did it? Thanks – barfoon May 12 '10 at 16:37
  • @barfoon If the reachability shows a network is available, I open a URL connection to a known location. If this fails, I display a message to the user. – brianegge May 13 '10 at 12:54
  • 1
    SBUsesNetwork appears to be a 'folklore' setting that actually isn't required -- it's possibly even a private API key, technically. Check this out: http://blogs.oreilly.com/iphone/2008/11/hunting-down-infoplist-prefere.html Someone found it through reverse engineering something. I presume it snowballed from there into a 'required' setting. Apple having nothing to say about it, so I think it is the stuff of myth. – occulus Feb 21 '11 at 13:12
  • The Apple message will indicate that the app will use 'cached data'. If that is ambiguous for your app, then you might not want to use this method. – ed_is_my_name Jul 23 '15 at 18:09
28

Apple Developer Connection has a sample application (Reachability) that uses the System Configuration framework to determine network status. It will tell you whether you have a WiFi, EDGE/3G or no Internet connection.

You would use portions of this code in your application to determine network state, and then provide interface cues if no connection is available, such as a UIAlertView.

Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
  • 2
    I made a note about Reachability here: http://stackoverflow.com/questions/181485/determining-when-an-edge-connection-comes-back-after-a-dropout-on-an-iphone – Chris Lundie Feb 27 '09 at 21:43
  • 1
    There's a nice reworking of the Apple Reachability sample here: http://blog.ddg.com/?p=24 It's designed to be a like-for-like drop-in. He's taken some care with it (way more than Apple) and I've successfully used his Reachability implementation without any issues. Note that if you use the ASIHTTP library, the abovementioned Reachability rewrite comes as part of that. – occulus Feb 21 '11 at 17:35
  • i am using reachability but it gives problem. because it shows alert more then one time and it really irritates – Mashhadi Jul 10 '11 at 19:06
4

Cautionary word: beware SBUsesNetwork. I would personally love to know where SBUsesNetwork originally came from, because it's not mentioned anywhere in Apple's docs that I can find. When I add the key to my app's plist (as a boolean) and set to true, it doesn't seem to affect the behaviour of my app -- I get no warning about airplane mode, whether starting app completely afresh, or foregrounding a previous launch that was backgrounded.

My app has UIRequiresPersistentWifi set to true, which appears to also do the job people claim SBUsesNetwork does (plus other things!).

(I'm running iOS4.2.1 on an iPhone 4, XCode 3.2.5 64 bit).

occulus
  • 16,959
  • 6
  • 53
  • 76
  • Ah, this is interesting: http://blogs.oreilly.com/iphone/2008/11/hunting-down-infoplist-prefere.html -- the guy found SBUsesNetwork by reverese engineering stuff. That SBUsesNetwork key might actually be a private API thing which is not intended for public consumption? – occulus Feb 21 '11 at 12:05
  • Ah, OK, think I've cracked this now. UIRequiresPersistentWifi *does* do the job that people claim SBUsesNetwork does -- i.e. warns about airplane mode -- but you must understand that you only get airplane mode warning once for an application during one activation of airplane mode -- even if you properly kill and relaunch an app, if you were already warned about airplane mode before (and are still in airplane mode), it won't warn you again. So to summarise: SBUsesNetwork looks like a red herring, ignore it. (If anyone can point me at docs for SBUsesNetwork, please do.) – occulus Feb 21 '11 at 12:12