公司所做的項目,鎖屏監聽是為了60秒后,解鎖瓶后顯示【手勢解鎖】或【指紋驗證】;
第一步:AppDelegate.m 頭部導入
#import <notify.h>
#define NotificationLock CFSTR("com.apple.springboard.lockcomplete")
#define NotificationChange CFSTR("com.apple.springboard.lockstate")
#define NotificationPwdUI CFSTR("com.apple.springboard.hasBlankedScreen")
第二步:在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法內加入
以下代碼
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, screenLockStateChanged, NotificationLock, NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, screenLockStateChanged, NotificationChange, NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
第三步:在appDelege.m中加入新的方法(C語言的)
static void screenLockStateChanged(CFNotificationCenterRef center,void* observer,CFStringRef name,const void* object,CFDictionaryRef userInfo)
{
NSString* lockstate = (__bridge NSString*)name;
if ([lockstate isEqualToString:(__bridge NSString*)NotificationLock]) {
NSLog(@"locked.");
// 此處監聽的系統鎖屏
} else {
NSLog(@"lock state changed.");
// 此處監聽到屏幕解鎖事件(鎖屏也會掉用此處一次,鎖屏事件要在上面實現)
}
}
第四步:如何在C語言函數內調用OC方法 ( C語言函數內沒法使用self )
本例為了實現在appDelegate.m中通過self 調用一個方法(彈出手勢解鎖的方法)
本質是通過指針來實現
1. 聲明一個全局變量,並賦nil
AppDelegate *appDelegate = nil;
2. 在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法內如下賦值:
appDelegate = self;
3.在剛才的鎖屏監聽的C語言函數內如下調用appDelegate OC方法,這樣就不會因為self導致報錯了
[appDelegate showGestureOrFinger];