doorxp

Blog

iOS应用内监听wifi的变化

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 {
    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
                                    NULL,
                                    &onNotifyCallback,
                                    CFSTR(kNotifySCNetworkChange),
                                    NULL,
                                    CFNotificationSuspensionBehaviorDeliverImmediately);
}
+ (void)stopMonitorWifiChange {
    CFNotificationCenterRemoveObserver(CFNotificationCenterGetDarwinNotifyCenter(),
                                       NULL,
                                       CFSTR(kNotifySCNetworkChange),
                                       NULL);
}


需要:

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

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

/*
 * 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  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 的更多信息可以参考:

发表评论:

«   2024年11月   »
123
45678910
11121314151617
18192021222324
252627282930
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
搜索
最新留言

    Powered By Z-BlogPHP 1.5.1 Zero

    Copyright doorxp.com Rights Reserved.