【发布时间】:2015-12-17 22:45:53
【问题描述】:
我想让我的应用程序在手机被锁定和解锁时监控,以及当它变成空白时(长时间不活动后),而这一切都是在我的应用程序没有集中注意力,而是在后台运行的时候。
我可以在应用获得焦点时轻松接收锁定/解锁/空白事件:
-(void) startListeningForPhoneLockEvent
{
NSLog(@"Start listening to lock/unlock and screen-goes-black events.");
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
(void*)self,
lockStateChanged,
CFSTR("com.apple.springboard.lockstate"),
NULL,
CFNotificationSuspensionBehaviorDeliverImmediately);
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
(void*)self,
hasBlankedScreen,
CFSTR("com.apple.springboard.hasBlankedScreen"),
NULL,
CFNotificationSuspensionBehaviorDeliverImmediately);
}
还有回调函数:
static void lockStateChanged( CFNotificationCenterRef center, void*observer, CFStringRef name, const void *object, CFDictionaryRef userInfo )
{
NSLog(@"Lock event received!");
}
static void hasBlankedScreen( CFNotificationCenterRef center, void*observer, CFStringRef name, const void *object, CFDictionaryRef userInfo )
{
NSLog(@"Blanked screen event received!");
}
我已启用后台模式:
- 后台提取。
但是,一旦应用进入后台,它就不会收到锁定/解锁/黑屏事件。
我已尝试使用其他后台模式,例如声音播放、位置更新等,但应用在后台时仍无法接收锁定/解锁/黑屏事件。
我不确定这是否真的可行,或者我做错了什么。
我正在使用最新的 XCode 和 iOS9 SDK 在更新到 iOS9 的真实设备上对其进行测试。
【问题讨论】:
-
我也不介意 Swift 中的解决方案。
-
仅在应用程序中启用后台模式并没有帮助,应用程序实际上应该在后台运行。当您锁定/解锁手机时,您能否确认您的应用程序确实在后台运行?
-
@SumantHanumante,Apple 对后台运行、监听锁定、解锁事件有任何限制吗?
-
不幸的是,我目前无法访问物理设备,因此无法对其进行测试。但是您是否尝试让应用程序在后台运行,然后检查回调。让应用程序在后台运行的一种快速方法是在前台启动位置更新,然后在“位置更新”模式开启的情况下将应用程序置于后台。
-
那你搞清楚了吗?有什么方法可以监控设备锁屏吗?我的意思是当用户输入错误的密码时,我们的应用程序应该在后台状态下做出反应或输入一些通知或方法。是否可以在 iOS 中实现该功能?在android中是可能的,所以。有人知道该怎么做吗?请指导我完成这个。
标签: ios objective-c multithreading events