How to Detect Airplane Mode/ Flight Mode in IOS Swift

筅森魡賤 提交于 2019-12-05 21:35:30

问题


I need to detect weather IPhone is in Airplane mode or Not, i have done much research and i found no direct way to achieve that . My App has the functionality of Calling a Number and When an Iphone has Airplane mode he can't call.

so I need to show him my custom Screen to Notify that Airplane mode is enable.

it will be good if any one can explain how we can achieve it in an effective way.


回答1:


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.




回答2:


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.




回答3:


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.")
}


来源:https://stackoverflow.com/questions/37025377/how-to-detect-airplane-mode-flight-mode-in-ios-swift

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