How to determine Internet Connection in Cocoa?

后端 未结 3 835
温柔的废话
温柔的废话 2020-12-18 05:38

I need to determine if Internet Connection is available or not. I don\'t care how it is connected (WI-FI, Lan,etc..) . I need to determine, is Internet Connection available

3条回答
  •  情深已故
    2020-12-18 06:08

    One way to do it is :

    // Check whether the user has internet
    - (bool)hasInternet {
        NSURL *url = [[NSURL alloc] initWithString:@"http://www.google.com"];
        NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:5.0];
        BOOL connectedToInternet = NO;
        if ([NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]) {
            connectedToInternet = YES;
        }
        //if (connectedToInternet)
            //NSLog(@"We Have Internet!");
        [request release];
        [url release];
        return connectedToInternet;
    }
    

提交回复
热议问题