码迷,mamicode.com
首页 > 其他好文 > 详细

OSX 监听系统网络设置

时间:2015-05-09 16:17:16      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

由于日常开发的需求,我们需要监听OSX一些系统设置的变化,达到软件程序设置的同步,这时我们可以通过以下函数监听系统设置的改变:

    #include <SystemConfiguration/SystemConfiguration.h>

//------------------------------------------------------------------
    SCDynamicStoreRef dynStore;
    SCDynamicStoreContext context = {0, this, NULL, NULL, NULL};
    dynStore = SCDynamicStoreCreate(kCFAllocatorDefault,
                                    CFBundleGetIdentifier(CFBundleGetMainBundle()),
                                    SystemProxyChangeCallBack,//自定义回调函数,备注【1】
                                    &context);
    const CFStringRef keys[3] = {CFSTR("State:/Network/Global/Proxies")};//获取系统代理设置,备注【2】
    CFArrayRef watchedKeys = CFArrayCreate(kCFAllocatorDefault,
                                           (const void **)keys,
                                           1,
                                           &kCFTypeArrayCallBacks);
    if (SCDynamicStoreSetNotificationKeys(dynStore, NULL, watchedKeys))
    {
        CFRelease(watchedKeys);
        CFRunLoopSourceRef rlSrc = SCDynamicStoreCreateRunLoopSource(kCFAllocatorDefault, dynStore, 0);
        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

 

OSX 监听系统网络设置

标签:

原文地址:http://www.cnblogs.com/sz-leez/p/4490101.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!