iOS 检测网络切换

时间:2022-07-17 23:53:39

  CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center

                                    NULL, // observer

                                    onNotifyCallback, // callback

                                    CFSTR("com.apple.system.config.network_change"), // event name

                                    NULL, // object

                                    CFNotificationSuspensionBehaviorDeliverImmediately);

 

staticvoid onNotifyCallback(CFNotificationCenterRef center, void *observer, CFStringRef name, constvoid *object, CFDictionaryRef userInfo)

{

    NSString* notifyName = (__bridgeNSString*)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"]) {

}

}