如何检测iOS中的wifi网络变化 [英] How to detect wifi network change in iOS
问题描述
我正在开发 iOS 应用程序,我需要检测网络何时从 Wifi 连接更改为另一个 Wifi 连接或 Wifi 和 3G 之间.
I am working on iOS application and I would need to detect when network changes either from Wifi connection to another Wifi connection or between Wifi and 3G.
我尝试使用 Reachability 库,但它似乎没有检测到 Wifi 连接之间的变化.我可以使用什么?
I have tried using Reachability library but it seems it does not detect changes between Wifi connections. What can I use?
应用程序的目标是 App Store,所以我不能使用 Apple 的私有方法.
Target of the application would be App Store so I can't use private methods of Apple.
更新:经过一些测试后,我发现使用模拟器进行测试时效果很好.我收到通知没有任何问题.iphone的问题,也许?
UPDATE: After some testing I have found out that when testing using simulator it works perfectly. I get notifications without any problem. iphone problem, maybe?
提前致谢
推荐答案
请参考此链接 https://stackoverflow.com/a/19256197/1382157
另外一种方式,
- (BOOL)isReachable {
return [self isReachableViaWWAN] || [self isReachableViaWiFi];
}
- (BOOL)isReachableViaWWAN {// If this return true, means it is connected to 3g
return self.networkReachabilityStatus ==
AFNetworkReachabilityStatusReachableViaWWAN;
}
- (BOOL)isReachableViaWiFi { // If this return true, means it is connected to wifi
return self.networkReachabilityStatus ==
AFNetworkReachabilityStatusReachableViaWiFi;
}
确保正确初始化类并执行
make sure you initialize class properly and do
[self.manager.reachabilityManager startMonitoring];
这篇关于如何检测iOS中的wifi网络变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!