user1828883
user1828883

Reputation: 51

Check For Active Internet Connection (Mac App)

I have seen lots of tutorials on how to check for internet connection on iOS, but I am finding it very hard to do this on OSX. What I have so far is this whenever it tries to load a website and there is no internet (and when the app launches):

NSURL *scriptUrl = [NSURL URLWithString:@"http://apple.com"];
NSData *data = [NSData dataWithContentsOfURL:scriptUrl];
if (data)
    NSLog(@"Device is connected to the internet");
else
    NSLog(@"Device is not connected to the internet");
    [RetryInternetWindow makeKeyAndOrderFront:NSApp];

The only problem I have found is that it seems to think that I never have any internet connection and displays the window that I told it to. Is there a simple way to check for internet on a mac app?

Upvotes: 1

Views: 1586

Answers (1)

trojanfoe
trojanfoe

Reputation: 122468

You should be able to use the Reachability API for this. See this Apple Guide.

There is also a drop-in replacement on github.

Upvotes: 1

Related Questions