Capture Wi-Fi network changing event in iOS(在 iOS 中捕获 Wi-Fi 网络变化事件)
问题描述
当用户连接到 iOS 应用程序中的特定 WiFi 网络时,是否有任何方法可以捕获发生的事件.即使这可以使用任何不需要超级用户权限(越狱)的私有库来实现,也可以.我只是想捕捉连接的 SSID 的变化事件.
Is there any way to capture the event occurs when a user connects to a particular WiFi network in iOS app. It is fine even if this can be achieved using any private library which doesn't require super user privileges (jail break). I just want to capture the changing event of the connected SSID.
推荐答案
我建议简单地使用 Larme 发布的内容,并设置一个 NSTimer 每隔一秒左右检查一次,如果您检测到当前网络的 SSID 是什么改变,只需做你需要做的任何事情.请记住,更改 WiFi 网络并不是一蹴而就的事情,因此拥有 1 秒的分辨率也不错
I would recommend simply using what Larme posted, and setting up an NSTimer to check every second or so, what the SSID of your current network is, if you detect a change, simply do whatever you need to do. Keep in mind, changing WiFi networks is not something that happens instantaneously, so having a 1 second resolution is not bad
在 applicationDidFinishLoading
NSTimer *ssidTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(fetchSSIDInfo) userInfo:nil repeats:YES];
在 AppDelegate 中
- (id)fetchSSIDInfo {
NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();
NSLog(@"Supported interfaces: %@", ifs);
id info = nil;
NSString *ifnam = @"";
for (ifnam in ifs) {
info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
NSLog(@"%@ => %@", ifnam, info);
if (info && [info count]) { break; }
}
if ([info count] >= 1 && [ifnam caseInsensitiveCompare:prevSSID] != NSOrderedSame) {
// Trigger some event
prevSSID = ifnam;
}
return info;
}
类似的东西.我无法检查代码是否没有错字,因为我不在 Mac 前,但应该不会太不同
Something like that. I can not check if code is typo free as I am not in front of a mac, but it should not be too different
这篇关于在 iOS 中捕获 Wi-Fi 网络变化事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 iOS 中捕获 Wi-Fi 网络变化事件


- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01