第一步:AppDelegate.m 頭部導(dǎo)入#import#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 方法內(nèi)加入
以下代碼
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.");
// 此處監(jiān)聽的系統(tǒng)鎖屏
} else {
NSLog(@"lock state changed.");
// 此處監(jiān)聽到屏幕解鎖事件(鎖屏也會掉用此處一次,鎖屏事件要在上面實(shí)現(xiàn))
}
}
第四步:如何在C語言函數(shù)內(nèi)調(diào)用OC方法? ( C語言函數(shù)內(nèi)沒法使用self )
本例為了實(shí)現(xiàn)在appDelegate.m中通過self 調(diào)用一個方法(彈出手勢解鎖的方法)
本質(zhì)是通過指針來實(shí)現(xiàn)
1. 聲明一個全局變量灶轰,并賦nil
AppDelegate *appDelegate = nil;
2. 在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法內(nèi)如下賦值:
appDelegate = self;
3.在剛才的鎖屏監(jiān)聽的C語言函數(shù)內(nèi)如下調(diào)用appDelegate OC方法,這樣就不會因?yàn)閟elf導(dǎo)致報(bào)錯了
[appDelegate showGestureOrFinger];