由于日常开发的需求,我们需要监听OSX一些系统设置的变化,达到软件程序设置的同步,这时我们可以通过以下函数监听系统设置的改变:
#include <SystemConfiguration/SystemConfiguration.h> SCDynamicStoreRef dynStore;
SCDynamicStoreContext context = {, this, NULL, NULL, NULL};
dynStore = SCDynamicStoreCreate(kCFAllocatorDefault,
CFBundleGetIdentifier(CFBundleGetMainBundle()),
SystemProxyChangeCallBack,//自定义回调函数,备注【1】
&context);
const CFStringRef keys[] = {CFSTR("State:/Network/Global/Proxies")};//获取系统代理设置,备注【2】
CFArrayRef watchedKeys = CFArrayCreate(kCFAllocatorDefault,
(const void **)keys,
,
&kCFTypeArrayCallBacks);
if (SCDynamicStoreSetNotificationKeys(dynStore, NULL, watchedKeys))
{
CFRelease(watchedKeys);
CFRunLoopSourceRef rlSrc = SCDynamicStoreCreateRunLoopSource(kCFAllocatorDefault, dynStore, );
CFRunLoopAddSource(CFRunLoopGetCurrent(), rlSrc, kCFRunLoopDefaultMode);
CFRelease(rlSrc);
}
else
{
CFRelease(watchedKeys);
CFRelease(dynStore);
dynStore = NULL;
}
CFRunLoopRun(); //在程序中需要调用该函数监听效果
备注【1】,当系统设置有所改变时,会触发该回调函数,可根据具体需要做相关的操作处理
void SystemProxyChangeCallBack(SCDynamicStoreRef store, CFArrayRef changedKeys,void *info)
{
}
备注【2】,这里demo程序为监听网络代理改变属性的设置,其他属性值可参考:
https://developer.apple.com/library/mac/documentation/Networking/Conceptual/SystemConfigFrameworks/SC_UnderstandSchema/SC_UnderstandSchema.html