美文网首页
iOS swift 检查网络的状态

iOS swift 检查网络的状态

作者: 我会回来的 | 来源:发表于2021-09-10 10:45 被阅读0次

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  是在有网的条件下。 

相关文章

网友评论

      本文标题:iOS swift 检查网络的状态

      本文链接:https://www.haomeiwen.com/subject/uvegwltx.html