在Swift中使用Apple的可达性类 [英] Using Apple's reachability class in Swift
本文介绍了在Swift中使用Apple的可达性类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在将现有的Objective-C代码(iOS)重写为 Swift 现在正遇到Apple Reachability
类用于检查网络可用性的问题...在我现有的代码中,我使用以下代码来实现这一目的.
I am rewriting my existing Objective-C code (iOS) to Swift and now am facing some issues with the Reachability
class of Apple for checking network availability... In my existing code I am using the following for achieving that.
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
:
And I am getting this error: network status is not convertible to string
at this line:
if (internetStatus != NotReachable)
是否存在获取网络状态的方法或类?
Is there a method or class for getting network status?
我需要满足以下三个条件:
I need these three conditions:
NotReachable: Obviously, there’s no Internet connection
ReachableViaWiFi: Wi-Fi connection
ReachableViaWWAN: 3G or 4G connection
推荐答案
对于网络可用性(在Swift 2中有效):
For network availability (works in 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)
这篇关于在Swift中使用Apple的可达性类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文