Nat*_*ate 16
一种方法是com.apple.system.config.network_change从Core Foundation Darwin通知中心收听事件.
注册活动:
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
NULL, // observer
onNotifyCallback, // callback
CFSTR("com.apple.system.config.network_change"), // event name
NULL, // object
CFNotificationSuspensionBehaviorDeliverImmediately);
Run Code Online (Sandbox Code Playgroud)
这是一个示例回调:
static void onNotifyCallback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
NSString* notifyName = (NSString*)name;
// this check should really only be necessary if you reuse this one callback method
// for multiple Darwin notification events
if ([notifyName isEqualToString:@"com.apple.system.config.network_change"]) {
// use the Captive Network API to get more information at this point
// https://stackoverflow.com/a/4714842/119114
} else {
NSLog(@"intercepted %@", notifyName);
}
}
Run Code Online (Sandbox Code Playgroud)
例如,请参阅我关于如何使用Captive Network API获取当前SSID的另一个答案的链接.
请注意,虽然我测试过的手机是越狱(iOS 6.1),但我认为这不需要越狱才能正常工作.它当然不需要将应用程序安装在正常的沙箱区域(/var/mobile/Applications/*)之外.
PS我没有详尽地测试这个事件,以了解此事件是否给出任何误报(基于您对网络更改的定义).但是,只要存储一些状态变量(等于最后一个网络的SSID),并且只要此事件发生,就将其与当前变量进行比较,这很简单.
| 归档时间: |
|
| 查看次数: |
7685 次 |
| 最近记录: |