我是使用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",
¬ify_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");
}
}
);
}
答案 0 :(得分:6)
在app delegate #import <notify.h>
在didFinishLaunchingWithOptions
中写下这段代码int notify_token;
notify_register_dispatch("com.apple.springboard.lockstate",
¬ify_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被锁定,您将获得&#34;锁定设备&#34;作为日志。所以你可以在那个块中编写你的代码。这对你有帮助。
答案 1 :(得分:3)
你不能在iPhone上这样做。 但通过,Darwin notifications。您可以通过“com.apple.springboard.lockcomplete”锁定设备时检测事件。
看看这些链接,希望它可以帮到你:
2)How can I detect screen lock/unlock events on the iPhone?
applicationWillSuspend 方法本身不存在,但在AppDelegate.m中您可以使用 applicationWillResignActive 和 applicationWillResignActive 这些方法将被调用当用户点击主页按钮并且应用程序将转到后台时(此处您可以保持连接状态,但是您应该阅读有关后台任务的Apple文档,因为如果应用程序保留在后台,您的连接将无法永久存在。是保持您的应用程序最新的其他方法,如更新推送通知等):
- (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:.
}
您可以在这些方法中处理您的连接。