How to tell with Objective-C if the iPhone is connected to a wifi network?

老子叫甜甜 提交于 2019-12-21 09:12:36

问题


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


回答1:


Check out the Reachability sample.




回答2:


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:

if ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != ReachableViaWiFi) {
    //Code to execute if WiFi is not enabled
}



回答3:


Look at the Reachability sample provided by Apple in the developer site. It does this.




回答4:


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.



来源:https://stackoverflow.com/questions/1279301/how-to-tell-with-objective-c-if-the-iphone-is-connected-to-a-wifi-network

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!