gpt4 book ai didi

ios - iPhone Objective-c 检测屏幕锁定

转载 作者:可可西里 更新时间:2023-11-01 04:21:57 25 4
gpt4 key购买 nike

我刚开始使用 Objective-c 制作 iPhone 应用程序

我想制作在 iPhone 屏幕锁定时发送通知的应用程序(按下锁定按钮)我怎样才能制作这个应用程序?

我正在尝试使用“applicationWillSuspend”实现它,但是

/*----------------------------------------*/
- (void)applicationWillSuspend
{
NSLog(@"WillSuspend");
}
/*----------------------------------------*/

此代码无效

我不确定何时调用 applicationWillSuspend

请给我一些知识

#import "AppDelegate.h"
#import <notify.h>

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// iOS8 Notification permit
if ([UIApplication
instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {
[[UIApplication sharedApplication]
registerUserNotificationSettings:[UIUserNotificationSettings
settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeSound
categories:nil]];
}
return YES;

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");
}
}
);

}

最佳答案

在应用委托(delegate)中导入这个 #import <notify.h>

把这段代码写在didFinishLaunchingWithOptions

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");
}
}
);

因此,一旦您的 iPhone 被锁定,您将获得“锁定设备”作为日志。所以你可以在那个 block 中编写你的代码。这会对您有所帮助。

关于ios - iPhone Objective-c 检测屏幕锁定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37649808/

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