Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if 'Mobile Network Data' is enabled or disabled (even when connected by WiFi) in iOS?

I have an app that I want to be able to use to get a connection status report after some certain time interval. Even when I am connected or associated to a Wifi network, I would like to know if data access is enabled over cellular network. This means, if after a check, I can gracefully disassociate from the wifi network, knowing that there is an available cellular connection to which the device will get connected to.

Current Reachability methods will only give me information about the availability of cellular only when am connected to that and there is not much information about getting this data before actually connecting to the interface.

Looking for a similar solution as available in android, as explained in this link.

CLARIFICATION

I am NOT looking to see if my device is having cellular capabilities. I AM trying to establish whether or not the user has enabled / disabled Data access over mobile network, and would like to know this information even if I am connected to Wifi. User can turn this on and off by going to Settings.

like image 603
Shane D Avatar asked Mar 23 '17 07:03

Shane D


2 Answers

If you are targeting iOS 12 or later, Apple has introduced (as part of the Network framework) the NWPathMonitor class. You can (as I did) instantiate two different monitors, one for cellular and another one for wifi:

let wifiMonitor = NWPathMonitor(requiredInterfaceType: .wifi)
let cellularMonitor = NWPathMonitor(requiredInterfaceType: .cellular)

Supposing you have a class that keeps track of the two connection statuses with two simple Booleans, with the pathUpdateHandler properties you could do:

wifiMonitor.pathUpdateHandler = { path in
    self.isWifiConnected = path.status == .satisfied                
}
cellularMonitor.pathUpdateHandler = { path in 
    self.isCellularConnected = path.status == .satisfied
}

and then handle yourself as you prefer. Remember to kickoff the monitors by calling start(_:) and providing serial queues (I provided two separate queues); there's a quirk so that, once you cancel a monitor instance, you have to instantiate a new one because it cannot be restarted.

like image 119
Alessandro Martin Avatar answered Nov 15 '22 22:11

Alessandro Martin


There is no available api's by which the app can query whether mobile data is enabled. You can use CTCellularData's cellularDataRestrictionDidUpdateNotifier and restrictedState to know if user has enabled or disabled cellular data access for your application. That is the max iOS allows for an application. And even this is not reliable as if you remove the sim from the device it will still give you the earlier restricted state status.

like image 24
user2428552 Avatar answered Nov 15 '22 22:11

user2428552