Handle No Internet Connection Error Before Try to Parse the Result in Alamofire

后端 未结 7 1842
旧巷少年郎
旧巷少年郎 2020-12-30 03:04

How should I handle if there is an error occurs when there is no internet connection in Alamofire. I tried checking if data is nil or not but it does not work.

Below

相关标签:
7条回答
  • 2020-12-30 03:57

    Other way to check for internet connection existing

    import SystemConfiguration
    
    func connectedToNetwork() -> Bool {
    
            var zeroAddress = sockaddr_in()    
    
           zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
    
            zeroAddress.sin_family = sa_family_t(AF_INET)
    
          guard let defaultRouteReachability = withUnsafePointer(&zeroAddress, {
                SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0))
            }) else {
                return false
            }
            var flags : SCNetworkReachabilityFlags = []
    
            if SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) == false {
    
                return false
    
            }
    
            let isReachable = flags.contains(.Reachable)
    
            let needsConnection = flags.contains(.ConnectionRequired)
    
            return (isReachable && !needsConnection)
    
        }
    
    let hasInternet = connectedToNetwork()
    
    0 讨论(0)
提交回复
热议问题