2013-05-19 43 views
3

我试图检测用户何时锁定设备(与例如按Home键)。Apple允许监视跳板事件吗?

Found this

CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center 
            NULL, // observer 
            lockStateChanged, // callback 
            CFSTR("com.apple.springboard.lockstate"), // event name 
            NULL, // object 
            CFNotificationSuspensionBehaviorDeliverImmediately); 



static void lockStateChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) { 
    NSLog(@"event received!"); 
    // you might try inspecting the `userInfo` dictionary, to see 
    // if it contains any useful info 
    if (userInfo != nil) { 
     CFShow(userInfo); 
    } 
} 

我可以想像,com.apple.springboard.lockstate就像调用私有API?或者这很好吗?

回答

3

假设所有的CF ...功能都是公开的,你可能确定,但​​是在一个阴暗的地方肯定。如果Apple更改该字符串,则iOS的下一个版本可能会破坏您的代码。

我在一个类似的情况下做了一个批准的运输应用程序是为了避免直接使用字符串。创建一个字符串数组,然后使用NSString方法将它们与句点分隔符组合起来,而不是直接使用com.apple.springboard.lockstate。

YMMV