gpt4 book ai didi

ios - 如何使用 Swift 检查 iOS 设备是否被锁定/解锁?

转载 作者:搜寻专家 更新时间:2023-10-31 22:15:13 25 4
gpt4 key购买 nike

如何使用 Swift 检测锁定/解锁的 iOS 设备(如 Android 中的 SCRENON/SCREENOFF)

最佳答案

我使用以下方法创建相同的想法。

您需要使用 bridge 将 objective c 代码用于 swift。

Here is the Link for create the bridge between the Objective c to 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", &notify_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();

希望这对您有帮助。

关于ios - 如何使用 Swift 检查 iOS 设备是否被锁定/解锁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31429800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com