問題描述
當用戶連接到 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 網絡變化事件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!