我已经看到这个问题出现了几次,但我还没有真正看到明确的答案...
从 iOS 5 开始,我的应用程序似乎遇到了这样的问题:如果用户锁定屏幕,我的 NSTimer 在大约 20 秒后停止触发锁屏。如果用户锁定屏幕,这使得我的应用程序每次计时器触发时都会播放声音毫无意义。
自从我将设备更新到 iOS 5 后,才发生这种情况。
我的问题是 1. 是否有任何解决方法可以在更新的设备上使用相同的功能,或者 2. 如果您的应用程序没有收到差评,推荐的方法是什么?锁定时请勿运行。
I've seen this come up a few times but I haven't really seen a definite answer...
Since iOS 5 my application seems to run into the problem that if the user locks the screen my NSTimer stops firing after about 20 seconds in the lock screen. This makes my app which plays a sound every time the timer fires pointless if the user locks the screen.
This has only happened since I updated my device to iOS 5.
My question is that 1. Is there any workaround to having this same functionality work on updated devices or 2. What is the recommended approach to not receiving bad reviews if your app doesn't run while locked.
发布评论
评论(2)
当用户锁定屏幕时, 应用程序进入“非活动”状态。这与将应用程序置于后台类似但不相同。 Apple 的文档指出“该应用程序正在前台运行,但当前未接收事件。(尽管它可能正在执行其他代码。)”
但是,进入非活动状态后,iPhone 会完全进入睡眠状态,除非正在运行某些东西以保持运行状态。它处于活动状态...等待 NSTimer 触发显然不满足此标准。 上一个问题中有讨论。 出现一个解决方案,是在计时器触发时播放声音,并在此期间连续播放静音声音,以防止 iPhone 进入睡眠状态。这是一个黑客攻击,但至少在这种情况下并不是一个完全丑陋的黑客攻击,因为使用声音来保持 iPhone 唤醒以播放声音具有某种对称性。
之前链接的内容:
来自 Apple 关于实现长时间运行的后台任务的指南:
你在你的应用程序中实现这个吗?如果没有,那是你的问题。
关于#2:您可以更新应用程序的描述,以表明它在屏幕锁定的情况下不起作用,但更好的做法是让它按照用户的预期工作。 :)
When the user locks the screen, the app enters the 'inactive' state. This is similar to but not identical toputting your app into the background. Apple's documentation states "The app is running in the foreground but is currently not receiving events. (It may be executing other code though.)"
However, after entering the inactive state the iPhone does go completely to sleep, unless something is running to keep it active... waiting for an NSTimer to fire does not satisfy this criteria, apparently. There is discussion in this previous question. One solution, it would appear, is to play your sound when your timer fires, and in the interim, play a silent sound continuously in order to prevent the iPhone from sleeping. A hack, but at least in this case not a completely ugly hack since using a sound in order to keep the iPhone awake in order to play a sound has a kind of symmetry.
Previously linked content:
From Apple's guidelines regarding implementing long-running background tasks:
Are you implementing this in your app? If not, that's your issue.
Regarding #2: You could update the description of the app to indicate it does not work with the screen locked, but better would be just to make it so that it works as the user would expect. :)
在iOS5中,苹果引入了更多的后台任务模式,包括“音频”、“位置”、“voip”、“报亭内容”、“外部配件”和“蓝牙中央”。我认为您可以确定可以在应用程序中使用哪种模式。链接是 http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW24
In iOS5, Apple has introduced more background task modes, including "audio", "location", "voip", "newsstand-content", "external-accessory" and "bluetooth-central". I think you can determine which mode you can use in your app. The link is http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW24