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

2019-06-24 19:03发布

This question already has an answer here:

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.

2条回答
Luminary・发光体
2楼-- · 2019-06-24 19:17

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];
查看更多
太酷不给撩
3楼-- · 2019-06-24 19:40

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.

查看更多
登录 后发表回答