我如何使用Swift检测已锁定/未锁定的iOS设备(就像Android中的SCREENON / SCREENOFF)
yourproject-Bridging-Header
文件中,添加yourcontroller.h。#import "notify.h"
-(void)registerAppforDetectLockState {
int notify_token;
notify_register_dispatch("com.apple.springboard.lockstate", ¬ify_token,dispatch_get_main_queue(), ^(int token) {
uint64_t state = UINT64_MAX;
notify_get_state(token, &state);
if(state == 0) {
NSLog(@"unlock device");
} else {
NSLog(@"lock device");
}
NSLog(@"com.apple.springboard.lockstate = %llu", state);
UILocalNotification *notification = [[UILocalNotification alloc]init];
notification.repeatInterval = NSCalendarUnitDay;
[notification setAlertBody:@"Hello world!! I come becoz you lock/unlock your device :)"];
notification.alertAction = @"View";
notification.alertAction = @"Yes";
[notification setFireDate:[NSDate dateWithTimeIntervalSinceNow:1]];
notification.soundName = UILocalNotificationDefaultSoundName;
[notification setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
});
}
-(void)registerAppforDetectLockState;
var instanceOfCustomObject: LockViewController = LockViewController()
instanceOfCustomObject.registerAppforDetectLockState();
希望这能帮上很多忙。
UIApplicationDidEnterBackgroundNotification
进入后台和锁屏有一定的相关性。 - Rhythmic Fistman