如何确定Internet连接在Cocoa? [英] How to determine Internet Connection in Cocoa?

查看:95
本文介绍了如何确定Internet连接在Cocoa?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要确定Internet连接是否可用。我不在乎它是如何连接的(WI-FI,Lan等)。我需要确定,是Internet连接可用。

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 at all .

我找到了一种方法来检查WI-FI连接。但我不在乎它是如何连接的(我需要检查所有可以连接到互联网的方式)。

P.S. I found a way to check WI-FI connection. But I don't care how it is connected (I need to check all the ways that can be connected to Internet).

像(isConnected)

Something like (isConnected)

推荐答案

请参阅 SCNetworkReachability 参考。这是一个C API,所以它不像单一方法调用一样容易,但它是一个伟大的工作,通知你的应用程序时,一个特定的地址变得可通过网络或不可达。

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.

广义大纲是用 SCNetworkReachabilityCreateWithAddress SCNetworkReachabilityCreateWithName 创建一个对象,然后添加它到运行循环与 SCNetworkReachabilityScheduleWithRunLoop 。当可达性被确定并且它改变时,您提供的回调函数将被调用。您可以使用它来更新应用程序的状态。

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 示例应用,它显示了如何使用它(虽然它是为iOS而不是Mac OS X设计的)

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

这篇关于如何确定Internet连接在Cocoa?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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