Using Apple's reachability class in Swift

前端 未结 4 965
余生分开走
余生分开走 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 17:08

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

提交回复
热议问题