iOS swift 检查网络的状态
public classReachability{classfuncisConnectedToNetwork()->Bool{varzeroAddress=sockaddr_in()zeroAddress.sin_len=UInt8(sizeofValue(zeroAddress))zeroAddress.sin_family=sa_family_t(AF_INET)letdefaultRouteReachability=withUnsafePointer(&zeroAddress){SCNetworkReachabilityCreateWithAddress(nil,UnsafePointer($0))}varflags=SCNetworkReachabilityFlags()if!SCNetworkReachabilityGetFlags(defaultRouteReachability!,&flags){returnfalse}letisReachable=(flags.rawValue&UInt32(kSCNetworkFlagsReachable))!=0letneedsConnection=(flags.rawValue&UInt32(kSCNetworkFlagsConnectionRequired))!=0return(isReachable&&!needsConnection)}}
返回true 是在有网的条件下。
网友评论