美文网首页
IOS判断是否联网

IOS判断是否联网

作者: 蜗牛1992 | 来源:发表于2016-09-03 21:56 被阅读715次
    + (BOOL)ConnectedToNetwork
    {
        // Create zero addy
        struct sockaddr_in zeroAddress;
        bzero(&zeroAddress, sizeof(zeroAddress));
        zeroAddress.sin_len = sizeof(zeroAddress);
        zeroAddress.sin_family = AF_INET;
        
        // Recover reachability flags
        SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
        SCNetworkReachabilityFlags flags;
        
        BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
        CFRelease(defaultRouteReachability);
        
        if (!didRetrieveFlags)
        {
            printf("Error. Could not recover network reachability flags\n");
            return NO;
        }
        
        BOOL isReachable = ((flags & kSCNetworkFlagsReachable) != 0);
        BOOL needsConnection = ((flags & kSCNetworkFlagsConnectionRequired) != 0);
        return (isReachable && !needsConnection) ? YES : NO;
    }
    

    相关文章

      网友评论

          本文标题:IOS判断是否联网

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