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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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 {
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
NULL,
&onNotifyCallback,
CFSTR(kNotifySCNetworkChange),
NULL,
CFNotificationSuspensionBehaviorDeliverImmediately);
}

+ (void)stopMonitorWifiChange {
CFNotificationCenterRemoveObserver(CFNotificationCenterGetDarwinNotifyCenter(),
NULL,
CFSTR(kNotifySCNetworkChange),
NULL);
}

需要:

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
* File System notifications
* These advise clients of various filesystem events.
*/
#define kNotifyVFSMount "com.apple.system.kernel.mount"
#define kNotifyVFSUnmount "com.apple.system.kernel.unmount"
#define kNotifyVFSUpdate "com.apple.system.kernel.mountupdate"
#define kNotifyVFSLowDiskSpace "com.apple.system.lowdiskspace"
#define kNotifyVFSLowDiskSpaceRootFS "com.apple.system.lowdiskspace.system"
#define kNotifyVFSLowDiskSpaceOtherFS "com.apple.system.lowdiskspace.user"

/*
* 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 "com.apple.system.hostname"
#define kNotifySCNetworkChange "com.apple.system.config.network_change"

/*
* Time Zone change notification
* Sent by notifyd when the system's timezone changes.
*/
#define kNotifyTimeZoneChange "com.apple.system.timezone"

/*
* System clock change notification
* Sent when a process modifies the system clock using the settimeofday system call.
*/
#define kNotifyClockSet "com.apple.system.clock_set"

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

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

This notification center is used to cover the <notify.h> 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 的更多信息可以参考:

本文参考:

如何捕获连接某个无线网络的通知


目前已转行教育行业,欢迎加微信交流:CaryaLiu