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 网络变化事件


基础教程推荐
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01