標籤:
iOS 鎖屏判斷
應用程式的單例類對象中得到應用程式委託的對象
UIApplicationDelegate* myDelegate = [[UIApplication sharedApplication] delegate];
UIApplication 接收到所有的系統事件和生命週期事件時,都會把事件傳遞給UIApplicationDelegate進行處理,對於使用者輸入事件,則傳遞給相應的目標對象去處理。
- (void)applicationWillResignActive:(UIApplication *)application
通知委託應用程式將進入非使用中,在此期間,應用程式不接收訊息或事件。
這個方法在使用者鎖住螢幕時,也會調用。
與之相適應的是應用程式重新被使用者開啟時的委託 方法。另外常用的就是記憶體不足的系統警告,此時會調用應用程式委託類的applicationDidReceiveMemoryWarning()方法, 然後我們就可以試著釋放一些記憶體了。
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, updateEnabled, CFSTR("com.apple.iokit.hid.displayStatus"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
// 1狀態
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, lockComplete, CFSTR("com.apple.springboard.lockcomplete"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
// 2狀態
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, lockState, CFSTR("com.apple.springboard.lockstate"), 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);
}
static void lockComplete(CFNotificationCenterRef center, void* observer, CFStringRef name, const void* object, CFDictionaryRef userInfo) {
uint64_t state;
int token;
notify_register_check("com.apple.springboard.lockcomplete", &token);
notify_get_state(token, &state);
notify_cancel(token);
NSLog(@"lockComplete狀態:%llu",state);
}
static void lockState(CFNotificationCenterRef center, void* observer, CFStringRef name, const void* object, CFDictionaryRef userInfo) {
uint64_t state;
int token;
notify_register_check("com.apple.springboard.lockstate", &token);
notify_get_state(token, &state);
notify_cancel(token);
NSLog(@"lockState狀態:%llu",state);
}
iOS 鎖屏判斷