2014-09-19 18 views
13

Przepisuję mój istniejący kod Objective-C (iOS) na Swift i mam teraz do czynienia z niektórymi problemami z klasą Apple Reachability w celu sprawdzenia dostępności sieci ... W moim istniejącym kodzie używam następujące po to, aby to osiągnąć.Używanie klasy osiągalności Apple w Swift

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

i otrzymuję ten błąd: network status is not convertible to string na tej linii:

if (internetStatus != NotReachable) 

Czy istnieje metoda lub klasy dla uzyskania statusu sieci?

muszę te trzy warunki:

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

To może się przydać ... https://github.com/ashleymills/Reachability.swift –

+0

dodałem pełniejszą odpowiedź tutaj myślę: http://stackoverflow.com/a/29787876/210456 – Dragouf

+1

http://github.com/ashleymills/Reachability.swift ma teraz obsługę CocoaPod –

Odpowiedz

19

Dla dostępności sieci (działa w Swift 2):

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

przypadku połączenia Wi-Fi:

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

** let networkStatus: Int = reachability.currentReachabilityStatus(). RawValue ** NOT ** let networkStatus: Int = reachability.currentReachabilityStatus(). Wartość ** – lee

+3

jaki jest nagłówek do importu? –

+0

"import ReachabilitySwift" i pobierz z https://github.com/ashleymills/Reachability.swift lub używając kokapodów – Jason

0

Spróbuj poniżej kod

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

włóż ten kod do swojej aplikacjiDelegate, aby sprawdzić osiągalność.

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

Wystarczy użyć jak ten

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