iPhone Objective-c检测屏幕锁定

我是使用Objective-c制作iPhone App的新手

我想制作在iPhone屏幕锁定时发送通知的应用程序(按下锁定按钮)如何制作此应用程序?

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

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

此代码不起作用

我不确定何时调用applicationWillSuspend

拜托,给我一些知识

 #import "AppDelegate.h" #import  @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"); } } ); } 

在app delegate #import 导入它

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

你不能在iPhone上这样做。 但通过, 达尔文通知 。 您可以通过“com.apple.springboard.lockcomplete”锁定设备时检测事件。

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

1) 锁定解锁事件iphone

2) 如何在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:. } 

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