Prerak Sola
Prerak Sola

Reputation: 10019

Swift: Get Mobile Data permission state

I have an application which I was testing on iOS 10.3 since few days and it was working fine. I recently tested it on an iOS 12 device and it was not working as expected. The application was not able to connect to my server.

Upon investigation, I found that the "Mobile Data" switch was turned off for my application in Settings -> AppName. After turning it on, it started working perfectly.

So, given this scenario, is there a way I can determine the status of this switch from my code? If I can know the status and if it's off, I can redirect the user to the application setting using:

let urlObj = NSURL.init(string:UIApplicationOpenSettingsURLString)
if #available(iOS 10.0, *) {
    UIApplication.shared.open(urlObj as! URL, options: [ : ], completionHandler: { Success in

     })
} else {
      let success = UIApplication.shared.openURL(url as URL)
      print("Open \(url): \(success)")                            
}

P.S: I am not looking for a solution using Reachability as it is not completely reliable.

Upvotes: 1

Views: 1064

Answers (1)

SierraMike
SierraMike

Reputation: 1569

There is no way to check this setting in the latest iOS release. You have two options to deal with this: first is you check if device is not connected to WiFi, and also not in airplane mode. If server can not be reached it’s safe to assume that data is disabled.

Second option is just to present user errors saying the application is unable to reach the server and to change UI/notice in application accordingly to deal with any scenario where a network connection can not be reached.

CTCellularData is a potential option however as of iOS 11 has some down falls of how often the state is checked.

Upvotes: 0

Related Questions