iphone判断网络链接是否正常的代码详解手机开发

+(BOOL)connectedToNetwork{ 
    struct sockaddr_in zeroAddress; 
    bzero(&zeroAddress;, sizeof(zeroAddress)); 
    zeroAddress.sin_len = sizeof(zeroAddress); 
    zeroAddress.sin_family = AF_INET; 
       
    SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress;); 
    SCNetworkReachabilityFlags flags; 
       
    BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags;); 
    CFRelease(defaultRouteReachability); 
       
    if (!didRetrieveFlags) 
    { 
        NSLog(@"Error. Could not recover network reachability flags"); 
        return NO; 
    } 
       
    BOOL isReachable = flags & kSCNetworkFlagsReachable; 
    BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired; 
    return (isReachable && !needsConnection) ? YES : NO; 
}

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/3379.html

(0)
上一篇 2021年7月16日
下一篇 2021年7月16日

相关推荐

发表回复

登录后才能评论