我正在将现有的Objective- C代码(iOS)重写为Swift,现在正面临ReachabilityApple类用于检查网络可用性的问题…在现有代码中,我使用以下代码来实现这一目标。
Reachability
var reachability: Reachability = Reachability.reachabilityForInternetConnection() var internetStatus:NetworkStatus = reachability.currentReachabilityStatus() if (internetStatus != NotReachable) { //my web-dependent code } else { //There-is-no-connection warning }
我收到此错误:network status is not convertible to string 在此行:
network status is not convertible to string
if (internetStatus != NotReachable)
是否有获取网络状态的方法或类?
我需要以下三个条件:
NotReachable: Obviously, there’s no Internet connection ReachableViaWiFi: Wi-Fi connection ReachableViaWWAN: 3G or 4G connection
对于网络可用性(在Swift 2中有效):
class func hasConnectivity() -> Bool { let reachability: Reachability = Reachability.reachabilityForInternetConnection() let networkStatus: Int = reachability.currentReachabilityStatus().rawValue return networkStatus != 0 }
对于Wi-Fi连接:
(reachability.currentReachabilityStatus().value == ReachableViaWiFi.value)