UPDATE: As of some version of iOS 8 (8.3, maybe), GPS hardware can be turned on even in airplane mode (i.e., you can get GPS location even in airplane mode). This renders this question moot.
--
Is it possible to programatically determine if an iPhone is in Airplane Mode? I specifically want to know if it's in AirPlane Mode, as opposed to having (or not having) a network connection. This question has been asked a lot, but every answer I've seen has referred to Apple's Reachability code to determine if a network connection is available.
开发者_JAVA技巧I'm writing an app that uses the iPhone's GPS. At the moment, if Airplane Mode is on and my app is launched, my location manager object still appears to exist and still appears to be giving me a (cached?) location. The little GPS icon appears in the status bar to let me know that my app is determining a location.
I want to know if the phone's in Airplane Mode so that I can not initialise the location manager object and I can change my UI to indicate to the user that GPS functionality is not available.
The GPS functionality can obviously be used regardless of network connectivity, so a lack of network connectivity strikes me as being a poor proxy for Airplane Mode. I don't appear to get any specific errors (through locationManager: didFailWithError) when in Airplane Mode, just a generic error that I'm assuming could come from a number of sources.
Similar requirements for my app, except I knew in advance that it would most likely be used in a "no cell signal" environment (camping in a remote area) and that the user might put the device in airplane mode to conserve batteries, without realizing it shuts down GPS.
Since airplane mode does not result in an didFailWithError, and locationServicesEnabled returns yes, I set a timer for 60 seconds, and if no GPS reading is returned before the timer expires, I display a message to the user telling them that GPS data can't be read, and suggest they check that airplane mode is not enabled, and that they ensure they have a clear view of the sky.
I don't know if it is possible to get this information, at least using public classes, but if what you need is to alert the user that he's in Airplane Mode, so with limited app functionality, you may set in your Info.plist file the UIRequiresPersistentWiFi
property to "true". Then when Airplane mode is set you will get this message.
By the way note that this approach is better than just checking the Airplane Mode status, other than because it is system supported, as if the user enables Airplane mode and then re-enables Wi-Fi (still staying in Airplane Mode) then the system alert will not be displayed.
One solution might be to take a screenshot of the device and look at the colors present in that region. If they're orange, you're in airplane mode.
You really shouldn't be trying to check if Airplane Mode is enabled or not. What if Apple makes a change to how Airplane Mode works, where it will leave the GPS equipment enabled even when turned on? The best way to reliably detect whether GPS services are enabled is to implement the CLLocationManagerDelegate
and let the APIs tell you whether GPS is available or not
Examining the NSError
it may pass to you will let you know if there are problems to
精彩评论