2

I am implementing a method in iOS app that allow user to download content over WiFi only or use both WiFi and 3G network to download content from web server, my question is how to make switch that can be turn on and off for downloading WiFi or 3G only option? just like apple use it for iTunes Store content on iOS, so if user turn wifi only option on than content will only be downloaded using wifi network.

Should i be using Reachability class or something else?

Dixit
  • 67
  • 1
  • 1
  • 8
  • I believe that there is an option in the iOS settings app that handles this kind of thing. The user can turn this setting ON to allow apps that can't use Wi-Fi to download over Cellular. To find this (Cellular enabled devices only), go to Settings>General>Cellular>Wi-Fi Plus Cellular – Sam Spencer Aug 22 '12 at 02:10
  • you right, but how can i have option inbuilt into my app that user can turn ON and OFF, just like many iOS application does eg: google+ app use this option. – Dixit Aug 22 '12 at 02:12
  • I'll have to look at the Google+ app. Look at exactly how they use it and in what context, because Apple doesn't exactly greet apps that toggle system settings with a warm welcome. Also, it may be possible to toggle bluetooth and Wi-Fi (3G may not be allowed): http://stackoverflow.com/questions/4518406/is-there-a-way-to-toggle-bluetooth-and-or-wifi-on-and-off-programatically-in-ios – Sam Spencer Aug 22 '12 at 02:17
  • mmm... on iPhone we have switch called use cellular data which use can turn ON and OFF, means if you turn it ON that content will be download using 3G & WiFi network otherwise only on WiFi network, that's what im tryi9ng to implement. Thanks :) – Dixit Aug 22 '12 at 02:23

2 Answers2

3

The Reachability class can help you with this. Initialize it as needed for your connectivity, eg:

Reachability *reachability = [Reachability reachabilityWithHostName:@"www.google.com"];

You can now query the 'currentReachabilityStatus' property to determine if you are connected, and if WiFi is available.

NetworkStatus status = reachability.currentReachabilityStatus;
switch(status)
{
    case ReachableViaWiFi:
        // There's a wifi connection, go ahead and download
        break;
    case ReachableViaWWAN:
        // There's only a cell connection, so you may or may not want to download
        break;
    case NotReachable:
        // No connection at all! Bad signal, or perhaps airplane mode?
        break;
}

Of course it's up to you to handle the states correctly in your application.

Nicholas M T Elliott
  • 3,643
  • 2
  • 19
  • 15
1

Also, you can change URLSessionConfiguration's property var allowsCellularAccess: Bool

the property turned to false, disallows session to load data via cellular

check more here

Alexey Lysenko
  • 1,890
  • 2
  • 12
  • 28