ios 在 Swift 中使用 Apple 的可达性类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25928826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Apple's reachability class in Swift
提问by Sam
I am rewriting my existing Objective-C code (iOS) to Swiftand 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.
我正在将现有的 Objective-C 代码 (iOS) 重写为Swift,现在在Reachability
检查网络可用性方面遇到了 Apple 类的一些问题......在我现有的代码中,我使用以下代码来实现这一点。
var reachability: Reachability = Reachability.reachabilityForInternetConnection()
var internetStatus:NetworkStatus = reachability.currentReachabilityStatus()
if (internetStatus != NotReachable) {
//my web-dependent code
}
else {
//There-is-no-connection warning
}
And I am getting this error: network status is not convertible to string
at this line:
我收到此错误:network status is not convertible to string
在这一行:
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
回答by Ramesh
For network availability (works in Swift 2):
对于网络可用性(适用于 Swift 2):
class func hasConnectivity() -> Bool {
let reachability: Reachability = Reachability.reachabilityForInternetConnection()
let networkStatus: Int = reachability.currentReachabilityStatus().rawValue
return networkStatus != 0
}
For a Wi-Fi connection:
对于 Wi-Fi 连接:
(reachability.currentReachabilityStatus().value == ReachableViaWiFi.value)
回答by Yalamandarao
Try below code
试试下面的代码
let connected: Bool = Reachability.reachabilityForInternetConnection().isReachable()
if connected == true {
println("Internet connection OK")
}
else
{
println("Internet connection FAILED")
var alert = UIAlertView(title: "No Internet Connection", message: "Make sure your device is connected to the internet.", delegate: nil, cancelButtonTitle: "OK")
alert.show()
}
回答by Abhimanyu Rathore
put this code in to your appDelegate for checking reachability .
将此代码放入您的 appDelegate 以检查可达性。
//MARK: reachability class
func checkNetworkStatus() -> Bool {
let reachability: Reachability = Reachability.reachabilityForInternetConnection()
let networkStatus = reachability.currentReachabilityStatus().rawValue;
var isAvailable = false;
switch networkStatus {
case (NotReachable.rawValue):
isAvailable = false;
break;
case (ReachableViaWiFi.rawValue):
isAvailable = true;
break;
case (ReachableViaWWAN.rawValue):
isAvailable = true;
break;
default:
isAvailable = false;
break;
}
return isAvailable;
}
回答by Sunil Sharma
Simply use like this
只需像这样使用
do {
let reachability: Reachability = try Reachability.reachabilityForInternetConnection()
switch reachability.currentReachabilityStatus{
case .ReachableViaWiFi:
print("Connected With wifi")
case .ReachableViaWWAN:
print("Connected With Cellular network(3G/4G)")
case .NotReachable:
print("Not Connected")
}
}
catch let error as NSError{
print(error.debugDescription)
}