在iOS中捕获Wi-Fi网络更改事件 [英] Capture Wi-Fi network changing event in iOS
问题描述
当用户连接到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
In 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网络更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!