在 iOS 中捕获 Wi-Fi 网络变化事件 [英] Capture Wi-Fi network changing event in iOS

查看:26
本文介绍了在 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 网络变化事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆