How can i know airplane mode on or off in ios [dup

2019-06-24 18:57发布

问题:

This question already has an answer here:

  • Detect airplane mode on iOS 5 answers

i would like to check airplane mode on or off on boolean variable. for example :

Bool airplaneMode = airplanemode;

if(airplaneMode)
{
     NSLoag(@"Airplane mode on");
}
else
{
    NSLoag(@"Airplane mode Off");
}

i don't want to check Network is available or not i need only to check airplane mode on or off.

回答1:

AFAIK, there is no built-in api to determine if Airplane mode is on. You have to check if the respective services are available, depending on what you need in your app.

For example, if you need a GPS location, you could check if a GPS location is available and then handle your different cases. Same for a network connection and all the other services that are disabled when airplane mode is on.

Rechability is one example for checking network connection.



回答2:

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];