+(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; 
}

发布评论

分享到:

IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

ios通过按钮点击异步加载图片代码详解
你是第一个吃螃蟹的人
发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。