如何跟踪 iPhone 的总使用情况,即使我们的应用程序在后台运行(未强制终止)。最近我遇到了应用程序,即 Moment此应用程序会跟踪您的 iPhone 使用情况,即使此应用程序在后台运行也是如此。实际上他们正在使用定位服务来获取执行时间。我的问题是当他们获得执行时间时,他们如何确定用户的 iPhone 屏幕是锁定还是解锁?
我有代码来检查屏幕是锁定还是解锁
-(void)registerAppforDetectLockState {
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");
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:@"unlock device" message:@"test" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
} else {
NSLog(@"lock device");
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:@"lock device" message:@"test" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}
});
}
但此代码仅在应用程序在前台运行时有效。
最佳答案
您的代码仅在应用程序在前台运行时才有效,因为当应用程序进入后台时,它不会被调用,您必须使用 background mode 之一唤醒您的应用程序可以执行您的代码。
关于ios - 如何像 'Moment' 应用程序那样跟踪 iPhone 使用情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32816470/