问题描述
我如何可以检测锁定/解锁iPhone上的事件?假设这只是对越狱的设备有可能,你可以点我到正确的API?
How can I detect lock/unlock events on the iPhone? Assuming it's only possible for jailbroken devices, can you point me to the correct API?
通过的锁定事件的,我的意思是显示或隐藏锁屏(这可能需要一个密码来解锁,或没有)。
By lock events, I mean showing or hiding the Lock Screen (which might need a password to unlock, or not).
推荐答案
您可以使用Darwin 的通知,监听的事件。从我对越狱的iOS 5.0.1的iPhone 4测试中,我认为这些事件之一可能是你所需要的:
You can use Darwin notifications, to listen for the events. From my testing on a jailbroken iOS 5.0.1 iPhone 4, I think that one of these events might be what you need:
com.apple.springboard.lockstate
com.apple.springboard.lockcomplete
注意:根据海报的,这应非越狱的手机上运行了。
Note: according to the poster's comments to a similar question I answered here, this should work on a non-jailbroken phone, too.
要使用此功能,注册这样的情况下(高于此注册了只是第一个事件,但你可以在 lockcomplete
添加一个观察者,太):
To use this, register for the event like this (this registers for just the first event above, but you can add an observer for lockcomplete
, too):
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
(void*)self, // observer (can be NULL)
lockStateChanged, // callback
CFSTR("com.apple.springboard.lockstate"), // event name
NULL, // object
CFNotificationSuspensionBehaviorDeliverImmediately);
其中, lockStateChanged
是事件的回调:
static void lockStateChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
NSLog(@"event received!");
if (observer != NULL) {
MyClass *this = (MyClass*)observer;
}
// you might try inspecting the `userInfo` dictionary, to see
// if it contains any useful info
if (userInfo != nil) {
CFShow(userInfo);
}
}
当设备处于锁定状态时,会出现 lockstate
事件和解锁,但 lockcomplete
事件只触发时设备将锁定。以确定事件是否为锁定或解锁事件的另一种方法是使用 notify_get_state()
。因为这里描述你的锁与解锁,得到不同的值。
The lockstate
event occurs when the device is locked and unlocked, but the lockcomplete
event is only triggered when the device locks. Another way to determine whether the event is for a lock or unlock event is to use notify_get_state()
. You'll get a different value for lock vs. unlock, as described here.
这篇关于锁定解锁事件iphone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!