如何使用 Objective-C 判断 iPhone 是否连接到 wifi 网络?
在 Objective-C、iPhone 应用程序的上下文中,我需要能够判断 iPhone 是否已连接到 wifi 网络,并且如果可能的话,监听此状态的变化。
有谁知道如何做到这一点?
谢谢, 加布伊
In the context of an Objective-C, iPhone application, I need to be able to tell whether the iPhone is connected to a wifi network, and if possible listen to changes in this state.
Does anyone know how to do this?
thanks,
gabouy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如其他人提到的,可达性示例是一个很好的选择然而,他们的答案并不足以满足我的口味。 在可达性示例中,代码是分散的,并且不向您展示如何处理最简单的情况。 对于最简单的情况(不监听状态的变化),我可以节省您自己整理正确行的精力。
将 Reachability 示例中的 Reachability 类复制到您的项目中,将其包含在您的类中,然后使用以下代码:
As others mentioned, the Reachablity sample is a good place to start, however, their answers don't spell it out enough for my tastes. Within the Reachablity sample the code is spread out and doesn't show you how to handle the simplest case. For the simplest case (not listening to changes in state) I can save you the effort of having to collate the right lines yourself.
Copy the Reachability class from the Reachablity sample into your project, include it in your class, and use the following code:
查看可达性示例。
Check out the Reachability sample.
查看 Apple 在开发者网站中提供的 Reachability 示例。 它就是这样做的。
Look at the Reachability sample provided by Apple in the developer site. It does this.
我建议使用 Reachability 示例项目苹果提供。 它涵盖了检查连接性所需的基本测试。
iPhone Developer Cookbook 中也有一个很好的例子。
I'd recommend using the Reachability sample project that Apple provides. It covers the base testing you need to do to check for connectivity.
There is also a good example in the iPhone Developer Cookbook.