Question

I am building an app that should only be used if airplane mode is activated. The app is for people that are not tech savvy to say the very least. Therefore, I cannot expect them to swipe up and click the airplane icon. What is the closest I can get to simply turning on airplane mode form within my app?

Basically, I am looking for the opposite of UIRequiresPersistentWifi.

Was it helpful?

Solution

There's currently no public API for direct checking whether the airplane mode is on.

The closest solution would be to use the SystemConfiguration framework and monitor whether the device can connect to the network.

Using the Reachability class (by tonymillion) you can do something like

Reachability* reach = [Reachability reachabilityWithHostname:@"www.google.com"];
reach.reachableBlock = ^(Reachability*reach) {
    // do something to prevent the app to be used
    // NOTE: this block is called on a background thread
    // so if you need to change the UI, do 
    dispatch_async(dispatch_get_main_queue(), ^{
        // UI related stuff
    });
};
reach.unreachableBlock = ^(Reachability*reach) {
    // ok continue using the app
};
[reach startNotifier];

OTHER TIPS

Well, I can give you an easier and faster technique. Run the the getIPaddresses method from this answer before asking the user, and save the info (or at least the count). You should see something like this:

"awdl0/ipv6" = "0:0:fe80::6456:66ff";
"en0/ipv4" = "192.168.1.4";
"en0/ipv6" = "0:0:fe80::4e5:772f";
"lo0/ipv4" = "127.0.0.1";
"lo0/ipv6" = "0:0:fe80::";
"pdp_ip0/ipv4" = "10.226.14.199";

These are all the active IP addresses. Now ask them to turn Airplane Mode on. When they tap yes, then run that code again, and the returned dictionary should be nil. If it isn't, and its the same as before, they lied!

I did look to see if Airplane mode would turn the interfaces OFF (interface->ifa_flags & IFF_UP) but it does not, and the two loopback addresses are always active.

You can do better than having the user teach you what airplane mode looks like, the pdp_ interfaces are the cellular interfaces, so if they're not there, then the phone is either in airplane mode or doesn't have cellular to begin with. This is good enough for my purposes, at least.

Here's what my code looks like in Xamarin:

        foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
        {
            if ((OperationalStatus.Up == ni.OperationalStatus) && (ni.Description.StartsWith("pdp_")))
                return false;
        }
        return true;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top