【发布时间】:2019-01-18 20:21:54
【问题描述】:
我需要区分连接到蜂窝网络和 wifi 的 iPhone/iPad 用户。在SCNetworkReachabilityFlag.isWWAN 下,Apple 表示:
The absence of this flag does not necessarily mean that a connection will never pass over a cellular network. If you need to robustly prevent cellular networking, read Avoiding Common Networking Mistakes in Networking Overview.
这意味着,如果启用此标志,则用户很可能通过蜂窝网络连接,但不能保证。我读了Avoiding Common Networking Mistakes,但没有帮助。
我怎样才能 100% 确定用户在蜂窝网络上(或不在)?
我的代码如下所示:
let reachability = SCNetworkReachabilityCreateWithName(nil, "localhost")
var flags = SCNetworkReachabilityFlags()
if let reachability = reachability {
SCNetworkReachabilityGetFlags(reachability, &flags)
}
let isConnectedToCellular = flags.contains(.isWWAN)
编辑:
如果您使用此代码,请务必在后台线程中调用它,而不要在主线程中调用。
【问题讨论】:
-
我怀疑这不是一个正确的问题。我假设您的意思是“我如何确保不通过蜂窝网络建立连接?”如果这是问题所在,Apple 的链接不正确(或者可能只是过时了)。它位于同一文档的“特定于平台的网络技术”中:developer.apple.com/library/archive/documentation/…
-
正确。谢谢。
标签: ios swift reachability