2014-09-19 14 views
13

Tôi đang viết lại mã Mục tiêu-C hiện tại (iOS) thành Swift và hiện đang gặp một số vấn đề với lớp học Reachability của Apple để kiểm tra tính khả dụng của mạng ... Trong mã hiện tại của tôi, tôi đang sử dụng sau đây để đạt được điều đó.Sử dụng lớp khả năng truy cập của Apple trong Swift

var reachability: Reachability = Reachability.reachabilityForInternetConnection() 
var internetStatus:NetworkStatus = reachability.currentReachabilityStatus() 
if (internetStatus != NotReachable) { 
    //my web-dependent code 
} 
else { 
    //There-is-no-connection warning 
} 

Và tôi nhận được lỗi này: network status is not convertible to string tại dòng này:

if (internetStatus != NotReachable) 

Có một phương pháp hoặc lớp để nhận tình trạng mạng?

Tôi cần ba điều kiện:

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

Điều này có thể được sử dụng ... https://github.com/ashleymills/Reachability.swift –

+0

tôi đã thêm một câu trả lời hoàn chỉnh hơn ở đây tôi nghĩ: http://stackoverflow.com/a/29787876/210456 – Dragouf

+1

http://github.com/ashleymills/Reachability.swift bây giờ có hỗ trợ CocoaPod –

Trả lời

19

Đối với mạng có sẵn (công trình trong Swift 2):

class func hasConnectivity() -> Bool { 
    let reachability: Reachability = Reachability.reachabilityForInternetConnection() 
    let networkStatus: Int = reachability.currentReachabilityStatus().rawValue 
    return networkStatus != 0 
} 

Đối với kết nối Wi-Fi:

(reachability.currentReachabilityStatus().value == ReachableViaWiFi.value) 
+0

** cho phép networkStatus: Int = reachability.currentReachabilityStatus(). RawValue ** NOT ** để cho networkStatus: Int = reachability.currentReachabilityStatus(). Value ** – lee

+3

tiêu đề cần nhập là gì? –

+0

"ReachabilitySwift" và tải xuống từ https://github.com/ashleymills/Reachability.swift hoặc sử dụng cocapods – Jason

0

Hãy thử dưới đây mã

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

đặt mã này vào ứng dụng của bạnĐăng ký để kiểm tra khả năng hiển thị.

//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

Đơn giản chỉ cần sử dụng như thế này

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) 
} 
Các vấn đề liên quan