Using Apple's reachability class in Swift

前端 未结 4 959
余生分开走
余生分开走 2020-12-08 16:33

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.

4条回答
  •  伪装坚强ぢ
    2020-12-08 16:59

    put this code in to your appDelegate for checking reachability .

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

提交回复
热议问题