Determining Internet Availability on iPhone?

前端 未结 2 1939
长发绾君心
长发绾君心 2021-01-15 18:41

I am using NSURLConnection in an iPhone app as so:

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest: request delegate: self];
2条回答
  •  既然无缘
    2021-01-15 19:17

    this works for me and is taken from apple seismic xml project:

    - (BOOL)isDataSourceAvailable
    {
        static BOOL checkNetwork = YES;
        if (checkNetwork) { // Since checking the reachability of a host can be expensive, cache the result and perform the reachability check once.
            checkNetwork = NO;
    
            Boolean success;    
            const char *host_name = "twitter.com"; // your data source host name
    
            SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, host_name);
            SCNetworkReachabilityFlags flags;
            success = SCNetworkReachabilityGetFlags(reachability, &flags);
            _isDataSourceAvailable = success && (flags & kSCNetworkFlagsReachable) && !(flags & kSCNetworkFlagsConnectionRequired);
            CFRelease(reachability);
        }
        return _isDataSourceAvailable;
    }
    

提交回复
热议问题