简体   繁体   English

如何使用Swift检查iPhone是否处于飞行模式

[英]How to check if iPhone is in Airplane Mode with Swift

I'd like to check whether flight mode is activated. 我想检查飞行模式是否启动。 If so, I need to show a warning message. 如果是这样,我需要显示一条警告消息。

How can I check whether flight (airplane) mode is active using Swift? 如何使用Swift查看飞行(飞机)模式是否处于活动状态?

The easiest way is to let iOS done it for you. 最简单的方法是让iOS为您完成。

to go Info.plist -> add this **Application uses Wi-Fi (Boolean) YES* 转到Info.plist->添加此**应用程序使用Wi-Fi(布尔值)是*

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 在iPhone iOS9上测试

在Xcode 7中

As far as I know you can't -- or you can't using public API. 据我所知您不能-或您不能使用公共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. 如果您确实需要了解其飞行模式,也许可以通过拨打3G,GPS,Wi-Fi等检查并检查每个飞行器是否关闭。

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: 如果您使用的是定位服务,则它们应通过在日志中显示以下内容来返回错误代码1009:

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. 如果您检查返回码-1009,则可以识别该用户为脱机用户,并进行相应的跟踪。

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. 在飞行模式下,它将返回0,而在网络模式下,它将返回强度。

Hope this will help you 希望这个能对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM