Objective-C检查网络是否可用的代码

SCNetworkReachabilityFlags flags;
    BOOL receivedFlags;
  
    SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(CFAllocatorGetDefault(), [@"google.com" UTF8String]);
    receivedFlags = SCNetworkReachabilityGetFlags(reachability, &flags);
    CFRelease(reachability);
  
    //NSLog(@"idiotic flags %@", receivedFlags);
  
    if (!receivedFlags || (flags == 0))
    {
        return NO;
    } else {
        return YES;
    }

编程技巧