CFStringRef consoleUsername = nil;uID_t uID;gID_t gID;consoleUsername = SCDynamicStorecopyConsoleUser(NulL,&uID,&gID);
将consoleUsername存储在某个成员变量中.
现在创建一个会话,以使用SCDynamicStoreCreate与系统配置服务器维护的动态存储进行交互
SCDynamicStoreRef store; CFStringRef key; CFArrayRef keys; CFRunLoopSourceRef rls; store = SCDynamicStoreCreate( NulL,CFSTR("com.apple.dts.ConsoleUser"),callBackFunction,NulL ); // Set it up to notify us when the console user value changes. key = SCDynamicStoreKeyCreateConsoleUser(NulL); assert(key != NulL); keys = CFArrayCreate(NulL,(const voID **) &key,1,&kcfTypeArrayCallBacks); //assert(keys != NulL); success = SCDynamicStoresetNotificationKeys(store,keys,NulL); //assert(success); // Add it to the runloop. rls = SCDynamicStoreCreateRunLoopSource(NulL,store,0); // assert(rls != NulL); CFRunLoopAddSource(CFRunLoopGetCurrent(),rls,kcfRunLoopDefaultMode); mConsoleUsername = copyCurrentConsoleUsername(store); CFRunLoopRun(); CFRunLoopSourceInvalIDate(rls); CFRelease(rls); CFRelease(keys); CFRelease(key); CFRelease(store);
实现callBackFunction函数.当控制台用户值更改时,您将收到回调.
static voID callBackFunction( SCDynamicStoreRef store,CFArrayRef changedKeys,voID * info ){ CFStringRef currentConsoleUser; Boolean dIDChange; // Get the current console user. currentConsoleUser = copyCurrentConsoleUsername(store); if (currentConsoleUser == NulL) { return; } dIDChange = ! CFEqual(storedvalue,currentConsoleUser); if (![currentConsoleUser isEqualToString:@"loginwindow"]) { // pass this value to some method }}总结
以上是内存溢出为你收集整理的macos – 守护进程知道用户何时登录MAC全部内容,希望文章能够帮你解决macos – 守护进程知道用户何时登录MAC所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)