检查活动的Internet连接(Mac App) [英] Check For Active Internet Connection (Mac App)

查看:94
本文介绍了检查活动的Internet连接(Mac App)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过很多关于如何在iOS上检查互联网连接的教程,但是我发现在OSX上很难做到这一点.到目前为止,我所遇到的是每当它尝试加载网站并且没有互联网(以及应用启动时)的情况:

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];


我发现的唯一问题是,似乎认为我从来没有任何互联网连接,并显示了告诉我的窗口.有没有一种简单的方法可以在Mac应用程序上检查互联网?


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?

推荐答案

您应该可以使用Reachability API.参见 Apple指南.

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

github 上也有一个替代项.

这篇关于检查活动的Internet连接(Mac App)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆