iPhone Objective-c检测屏幕锁定 [英] iPhone Objective-c detect Screen Lock

查看:233
本文介绍了iPhone Objective-c检测屏幕锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new to make iPhone App with Objective-c

我想制作一个可以在iPhone屏幕锁定时发送通知的应用(按下Lock键) 我该如何制作这个应用程式?

I want to make the App which sends a notification when iPhone screen is locked(Pressed Lock button) How can I make this app?

我正在尝试使用"applicationWillSuspend"来实现,但是

I'm trying to make it using "applicationWillSuspend", but

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

此代码无效

我不确定何时调用applicationWillSuspend

I'm not sure when applicationWillSuspend is called

请给我一些知识

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

}

推荐答案

将其导入应用程序委托#import <notify.h>

Import this in app delegate #import <notify.h>

在didFinishLaunchingWithOptions中编写这段代码

Write this piece of code in 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被锁定,您将获得锁定设备"作为日志.因此,您可以在该块中编写代码.这将为您提供帮助.

So once your iPhone gets locked, you will get "lock device" as log. So you can write your code in that block. This will help you.

这篇关于iPhone Objective-c检测屏幕锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆