测试环境:iOS6.1 device:iphone5
获取锁屏方式区分为2种方式:
第一种(需越狱):
if ([[objc_getClass("SBAwayController") sharedAwayController] isLocked]) {
NSLog(@"double check Home ,now YES Lock");
} else{
NSLog(@"double check Home ,now NO Lock");
}
这是copy的sina博客的。
第二种:
我们知道锁屏和解锁的时候,iOS系统会发送通知,根据搜索,大概有下面3种通知:
com.apple.iokit.hid.displayStatus
该通知系统日志会打印出来: Apr 27 14:50:47 Administratorteki-iPad backboardd[69] <Notice>: Posting 'com.apple.iokit.hid.displayStatus' notifyState=0具体分析就是锁屏后通知会发出消息,在屏幕变亮后,没有滑动解锁,系统也会发出该通知
com.apple.springboard.lockstate
该通知在系统锁屏和滑动解锁后,会发出该通知
com.apple.springboard.lockcomplete
对锁屏状态的获取,第一种方式是比较合适和简单的,如果想根据通知来获取,同样可以,代码如下:
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, updateEnabled, CFSTR("com.apple.iokit.hid.displayStatus"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
static void updateEnabled(CFNotificationCenterRef center, void* observer, CFStringRef name, const void* object, CFDictionaryRef userInfo) {
uint64_t state;
int token;
notify_register_check("com.apple.iokit.hid.displayStatus", &token);
notify_get_state(token, &state);
notify_cancel(token);
NSLog(@"%llu",state);
}
state 的值就是锁屏的状态值。