我的应用程序访问互联网,我只想检测是否有通过 wifi 或运营商数据网络连接
苹果为“可达性”做了一个例子
https://developer.apple.com/iphone/library/samplecode/Reachability/
我认为它只是检测到 wifi 而不是运营商网络
谁能告诉我,检测是否存在连接(任何类型的连接)最好的方法是什么
感谢您的帮助!
我的应用程序访问互联网,我只想检测是否有通过 wifi 或运营商数据网络连接
苹果为“可达性”做了一个例子
https://developer.apple.com/iphone/library/samplecode/Reachability/
我认为它只是检测到 wifi 而不是运营商网络
谁能告诉我,检测是否存在连接(任何类型的连接)最好的方法是什么
感谢您的帮助!
该样本正是您所需要的。
查看 Reachability.m。它会告诉你是否有任何联系,然后告诉你你有什么样的联系。
如果您只想检测您是否已连接,以及如果您已连接,您正在使用什么类型的连接,那么 Reachability 示例可能有点过分。实际上,该示例还包含显示如何设置和使用通知您状态更改的回调的代码。
有关确切告诉您是否已连接以及您使用的连接类型的源代码片段,您可能需要查看我对类似问题的回答,此处发布。
一旦你向任何网络资源发出请求,iPhone 就会使用它找到的任何连接,它使用 wifi(作为更高的优先级),如果 wifi 未连接,它使用运营商网络。没有阻止运营商网络的代码设置。
所有你需要做的:
这些技术很有用,但请记住,设备到 Internet 的路径可能包含几种不同类型的连接。例如,iPhone 或 iPod touch 可能使用 WiFi 连接到移动热点,而移动热点又拥有自己的 3G 无线连接。因此,假设 WiFi 连接的存在意味着比 3G 更高的带宽是不安全的。
Reachability (both Apple's sample code, and other people's similar code, and the underlying SCNetworkReachability will tell you either that there is no internet access, or that you will get internet access through WiFi, or that you will get internet access through mobile data.
If you have both WiFi and mobile data available, it will tell you "WiFi". There is no way to find out that mobile data is available when you have WiFi. (You can easily find out that mobile data is available when there is no WiFi). You cannot find out if Airplane mode is turned on (which would be different from WiFi and mobile data turned off).
WiFi available doesn't guarantee that a download will use WiFi and not mobile data. You might start a download at home, leave a minute later, WiFi disappears, and iOS will happily continue downloading through 3G. You can set a flag in the download call to disallow 3G. The error code will be -1009 = No internet access, no mention that 3G would have been available.
Using CTTelephonyNetworkInfo you can find out that you are on a device that could have mobile data (not an iPod Touch or iPad without mobile data, and there is a SIM in the device).