在 iOS 中捕获 Wi-Fi 网络变化事件

Capture Wi-Fi network changing event in iOS(在 iOS 中捕获 Wi-Fi 网络变化事件)
本文介绍了在 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 网络变化事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)