How to Detect Airplane Mode/ Flight Mode in IOS Swift

放肆的年华 提交于 2019-12-04 04:40:46

Here is another way to detect this mode like below,

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.

Try using SCNetworkReachabilityGetFlags (SystemConfiguration framework). If the flags variable handed back is 0 and the return value is YES, airplane mode is turned on.

Check out Apple's Reachability classes.

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.

If you are not submitting to the App Store, you can inspect the status bar using private API to see if the Airplane mode icon is present. This is checking the status bar view hierarchy to see if there is a UIStatusBarAirplaneModeItemView in the subviews.

In addition to not being App Store safe, this method also requires your app to show the status bar. This is also fragile and subject to break any time Apple decides to change the status bar structure.

if let statusBarSubviews = ((UIApplication.shared.value(forKey: "statusBar") as? UIView)?.value(forKey: "foregroundView") as? UIView)?.subviews,
    statusBarSubviews.contains(where: { $0.classForCoder == NSClassFromString("UIStatusBarAirplaneModeItemView") }) {
        print("Airplane mode is on.")
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!