Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we read Wireless Data permissions on iOS?

If internet is not reachable, is it possible to programmatically distinguish between the following permission cases?

  1. A case where user has denied permissions to use Data for the app.
  2. A case where user has granted permissions to use Data, but phone is in Flight Mode (or has no SIM/WiFi at all).

allow app to use data

I'd like to read those Wireless Data permissions to distinguish the above cases for more friendly error messages (i.e. in one case I would recommend to verify Permissions, and in the other case I would recommend to check Flight Mode)

All I could find is to get internet reachability, based on How to use SCNetworkReachability in Swift, but it doesn't know about actual permissions:

import SystemConfiguration

@available(iOS 9.0, macOS 10.11, *)
static func internetConnectionIsReachable() -> Bool {
    var zeroAddress = sockaddr_in6()
    zeroAddress.sin6_len = UInt8(MemoryLayout<sockaddr_in6>.size)
    zeroAddress.sin6_family = sa_family_t(AF_INET6)
    guard let reachability = withUnsafePointer(to: &zeroAddress, {
        $0.withMemoryRebound(to: sockaddr.self, capacity: 1) {
            SCNetworkReachabilityCreateWithAddress(nil, $0)
        }
    }) else {
        return false
    }
    var flags: SCNetworkReachabilityFlags = []
    guard SCNetworkReachabilityGetFlags(reachability, &flags) else {
        return false
    }
    return flags.contains(.reachable)
}
like image 583
Cœur Avatar asked May 17 '18 09:05

Cœur


2 Answers

In a word, you probably can't distinguish these cases, and you probably shouldn't want to. Apple doesn't even really want you to use reachability. Their view is: Yours not to reason why, yours but to do (i.e. try to use the network) or die (i.e. fail). Fail gracefully if you're going to fail, and you've done your job.

like image 139
matt Avatar answered Oct 13 '22 12:10

matt


As a general rule, you cannot read permissions directly. You can only determine capabilities (by which I mean "it fails when you try to do something"), which are sometimes set in ways other than user-controlable permissions, such as parental controls or restrictions imposed by an enterprise cert. You may get lucky here and there's a way, but my expectation would be 'no'.

like image 3
Rob Napier Avatar answered Oct 13 '22 10:10

Rob Napier