How to determine Internet Connection in Cocoa?

后端 未结 3 836
温柔的废话
温柔的废话 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 05:46

    Take a look at the SCNetworkReachability reference. This is a C API, so it's not as easy to use as a single method call, but it does a great job of notifying your app when a particular address becomes reachable or unreachable over the network.

    The broad outline is you'll create an object with SCNetworkReachabilityCreateWithAddress or SCNetworkReachabilityCreateWithName, and then add it to the run loop with SCNetworkReachabilityScheduleWithRunLoop. When the reachability is determined and when it changes, the callback function you supply will be called. You can use that to update the state of your application.

    Apple supplies an example app that shows how to use this (although it's designed for iOS, not Mac OS X)

    0 讨论(0)
  • 2020-12-18 06:06

    This code works for both iOS and OSX platforms, I hope.

    #include <SystemConfiguration/SystemConfiguration.h>
    static BOOL isInternetConnection()
    {
        BOOL returnValue = NO;
    
    #ifdef TARGET_OS_MAC
    
        struct sockaddr zeroAddress;
        bzero(&zeroAddress, sizeof(zeroAddress));
        zeroAddress.sa_len = sizeof(zeroAddress);
        zeroAddress.sa_family = AF_INET;
    
        SCNetworkReachabilityRef reachabilityRef = SCNetworkReachabilityCreateWithAddress(NULL, (const struct sockaddr*)&zeroAddress);
    
    
    #elif TARGET_OS_IPHONE
    
        struct sockaddr_in address;
        size_t address_len = sizeof(address);
        memset(&address, 0, address_len);
        address.sin_len = address_len;
        address.sin_family = AF_INET;
    
        SCNetworkReachabilityRef reachabilityRef = SCNetworkReachabilityCreateWithAddress(NULL, (const struct sockaddr*)&address);
    
    #endif
    
        if (reachabilityRef != NULL)
        {
            SCNetworkReachabilityFlags flags = 0;
    
            if(SCNetworkReachabilityGetFlags(reachabilityRef, &flags))
            {
                BOOL isReachable = ((flags & kSCNetworkFlagsReachable) != 0);
                BOOL connectionRequired = ((flags & kSCNetworkFlagsConnectionRequired) != 0);
                returnValue = (isReachable && !connectionRequired) ? YES : NO;
            }
    
            CFRelease(reachabilityRef);
        }
    
        return returnValue;
    
    }
    
    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
提交回复
热议问题