2014-09-19 38 views
13

我正在將我現有的Objective-C代碼(iOS)重寫爲Swift,現在我正面臨着與Apple的Reachability類有關的一些問題,用於檢查網絡可用性......在我現有的代碼中,我使用以實現這一目標。在Swift中使用Apple的可達性類

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在這一行:

if (internetStatus != NotReachable) 

是否有獲得網絡狀態的方法或類?

我需要這三個條件:

NotReachable: Obviously, there’s no Internet connection 
ReachableViaWiFi: Wi-Fi connection 
ReachableViaWWAN: 3G or 4G connection 
+1

這可能是有用的...... https://github.com/ashleymills/Reachability.swift – 2014-12-02 11:43:30

+0

我增加了一個更完整的答案在這裏,我想:HTTP:/ /stackoverflow.com/a/29787876/210456 – Dragouf 2015-04-22 04:46:52

+1

http://github.com/ashleymills/Reachability.swift現在有CocoaPod支持 – 2015-05-02 22:24:30

回答

19

網絡可用性(斯威夫特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) 
+0

** let networkStatus:Int = reachability.currentReachabilityStatus()。rawValue ** NOT ** let networkStatus:Int = reachability.currentReachabilityStatus()。value ** – lee 2015-08-04 06:19:50

+3

什麼是導入的頭文件? – 2016-09-01 13:51:39

+0

「導入ReachabilitySwift」並從https://github.com/ashleymills/Reachability.swift下載或使用cocapods – Jason 2016-10-09 17:53:25

0

下面嘗試代碼

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() 
     } 
0

將此代碼放入您的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; 
} 
0

只需使用這樣

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) 
}