How to check if iPhone is in Airplane Mode with Swift

大兔子大兔子 提交于 2019-12-02 04:07:37

The easiest way is to let iOS done it for you.

to go Info.plist -> add this **Application uses Wi-Fi (Boolean) YES*

To test: Kill your app -> turn on airplane mode -> open ur app: you should be able to see alert within the app. Tested on iPhone iOS9

As far as I know you can't -- or you can't using public API. My suggestion would be to call required devices and set corresponding notifications. If you really need to know if its flight mode, maybe you can do that with calling 3G, GPS, Wi-Fi etc. and check if every one is off.

I saw another suggestion with taking an in-app screenshot and checking for orange airplane ( does top contains orange part ).

Drawbacks are present for both, My suggestion is bad since your devices could be off for other, not related reasons.

Good luck.

If you are using Location Services, they should return with an error code of 1009 by presenting the following in log:

Geocode error: Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline."

If you check for a return code of -1009, you can recognize the user as offline and follow through accordingly.

You cant check the airplane mode is active or not , while you can check the cellular signal strength,

func getSignalStrength() -> Int {

let application = UIApplication.shared
let statusBarView = application.value(forKey: "statusBar") as! UIView
let foregroundView = statusBarView.value(forKey: "foregroundView") as! UIView
let foregroundViewSubviews = foregroundView.subviews

var dataNetworkItemView:UIView? = nil

for subview in foregroundViewSubviews {

    if subview.isKind(of: NSClassFromString("UIStatusBarSignalStrengthItemView")!) {
        dataNetworkItemView = subview
        break
    }
}

 if dataNetworkItemView == nil
 {
    return 0
 }
return dataNetworkItemView?.value(forKey: "signalStrengthBars") as! Int

} 

Using this method you can get the signal strength and determine the device is in airplane mode or not . In airplane mode it will return 0 while in network it will return strength.

Hope this will help you

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!