【问题标题】:iPhone Objective-c detect Screen LockiPhone Objective-c 检测屏幕锁定
【发布时间】:2016-06-06 04:59:07
【问题描述】:

我是使用 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");
                                 }
                             }
                             );

}

【问题讨论】:

标签: ios objective-c iphone appdelegate darwin


【解决方案1】:

在应用代理#import &lt;notify.h&gt;中导入这个

在 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 被锁定,您将获得“锁定设备”作为日志。因此,您可以在该块中编写代码。这会对你有所帮助。

【讨论】:

  • 我将代码添加到我的问题中,但它不起作用。 Xcode 说 notify_token 永远不会被执行。你能检查我的代码吗?
  • @Issei,您需要在 AppDelegate 中将notify_token 定义为 ivar
  • 你从哪里得到notify.h ??
  • notify.h不需要导入任何东西,直接使用即可。更多信息请访问developer.apple.com/library/content/documentation/Darwin/…
  • 感谢@Pushkraj 的快速回复,现在我通过将这段代码和平放在目标 c 文件中以便在 swift 中使用,经历了许多错误。你能分享更多关于如何正确使用它的信息吗?
【解决方案2】:

你不能在 iPhone 上做到这一点。 但是通过Darwin notifications。当设备被“com.apple.springboard.lockcomplete”锁定时,您可以检测到该事件。

看看这些链接也希望它可以帮助你:

1)Lock Unlock events iphone

2)How can I detect screen lock/unlock events on the iPhone?

applicationWillSuspend 方法本身并不存在,但在 AppDelegate.m 中你可以使用 applicationWillResignActiveapplicationWillResignActive 这些方法将被调用当用户点击主页按钮并且应用程序将转到后台时(在这里您可以保持连接,但您应该阅读有关后台任务的苹果文档,因为如果应用程序保持在后台,您的连接将无法永远存在。那里还有其他方法可以让您的应用保持最新状态,例如更新推送通知等):

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

当应用程序终止(从多任务处理完全关闭)时,将调用此方法。

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

您可以在这些方法中处理您的连接。

【讨论】:

  • 注意:仅当您在 PLIST 中显式启用 applicationWillTerminate 时才会调用它:默认情况下,iOS(和 OSX)只会 sigkill 应用程序(这比将控制权传回应用程序要快;它只会丢弃所有页面凭记忆)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-02
  • 1970-01-01
  • 1970-01-01
  • 2016-08-24
  • 2019-05-03
  • 2016-03-30
  • 1970-01-01
相关资源
最近更新 更多