如何检查iOS设备是否使用Swiftlocking/解锁?
我如何检测使用Swift的locking/解锁的iOS设备(如Android中的SCRENON / SCREENOFF)
我使用以下创build相同的想法。
你需要使用桥梁的客观c代码使用迅速。
这里是链接,以创buildObjective-C之间的桥梁Swift。
完成后,您可以将以下.h文件添加到yourproject-Bridging-Header
。 文件添加yourcontroller.h
然后将NotificationCenter.framework添加到您的项目中。
进入你的CustomObject.m
#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]; }); }
然后CustomObject.h
-(void)registerAppforDetectLockState;
现在进入swift代码,你可以直接使用这个方法。
var instanceOfCustomObject: LockViewController = LockViewController() instanceOfCustomObject.registerAppforDetectLockState();
这可能会帮助很多。