Apple 设备可在用户不感知的情况下进行wifi的切换,如果我们在App 内部需要感知设备wifi切换的变化,可以在 Darwin Notify Center 中注册监听 kNotifySCNetworkChange 通知,如下:

static void onNotifyCallback(CFNotificationCenterRef center,
                             void *observer,
                             CFStringRef name,
                             const void *object,
                             CFDictionaryRef userInfo) {
    if (CFStringCompare(name, CFSTR(kNotifySCNetworkChange), kCFCompareCaseInsensitive) == kCFCompareEqualTo) {
        //TODO when wifi changed
+ (void)startMonitorWifiChange {
+ (void)stopMonitorWifiChange {


#import "/usr/include/notify_keys.h"

上述代码中 kNotifySCNetworkChange 在该头文件中定义,跳至该头文件还可看到:

 * File System notifications
 * These advise clients of various filesystem events.
#define kNotifyVFSMount ""
#define kNotifyVFSUnmount ""
#define kNotifyVFSUpdate ""
#define kNotifyVFSLowDiskSpace ""
#define kNotifyVFSLowDiskSpaceRootFS ""
#define kNotifyVFSLowDiskSpaceOtherFS ""
 * System Configuration notifications
 * These advise clients of changes in the system configuration
 * managed by the system configuration server (configd).
 * Note that a much richer set of notifications are available to
 * clients using the SCDynamicStore API.
#define kNotifySCHostNameChange ""
#define kNotifySCNetworkChange ""
 * Time Zone change notification
 * Sent by notifyd when the system's timezone changes.
#define kNotifyTimeZoneChange ""
 * System clock change notification
 * Sent when a process modifies the system clock using the settimeofday system call.
#define kNotifyClockSet ""

由此可猜测在 Darwin Notify Center 中还可监听系统时区改变,系统时间改变,系统 hostname 改变的通知,未验证。

CFNotificationCenter Reference 中有关于 CFNotificationCenterGetDarwinNotifyCenter 的描述如下:

This notification center is used to cover the  Core OS notification mechanism (see /usr/include/notify.h). An application has only one Darwin notification center, so this function returns the same value each time it is called.

The Darwin Notify Center has no notion of per-user sessions, all notifications are system-wide. As with distributed notifications, the main thread’s run loop must be running in one of the common modes (usually kCFRunLoopDefaultMode) for Darwin-style notifications to be delivered.

由此可见, Darwin Notify Center 是系统级别,每次使用接口 CFNotificationCenterGetDarwinNotifyCenter 返回的都是同一个 Darwin Notify Center 实例。

关于 Darwin Notify Center 的更多信息可以参考:


Powered By Z-BlogPHP 1.5.1 Zero

Copyright Rights Reserved.