iOS 5 上屏幕锁定时不会发生 NSTimer 事件

发布于 2024-12-17 01:33:13 字数 266 浏览 2 评论 0 原文

我已经看到这个问题出现了几次,但我还没有真正看到明确的答案...

从 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

格子衫的從容 2024-12-24 01:33:13

当用户锁定屏幕时, 应用程序进入“非活动”状态。这与将应用程序置于后台类似但不相同。 Apple 的文档指出“该应用程序正在前台运行,但当前未接收事件。(尽管它可能正在执行其他代码。)”

但是,进入非活动状态后,iPhone 会完全进入睡眠状态,除非正在运行某些东西以保持运行状态。它处于活动状态...等待 NSTimer 触发显然不满足此标准。 上一个问题中有讨论。 出现一个解决方案,是在计时器触发时播放声音,并在此期间连续播放静音声音,以防止 iPhone 进入睡眠状态。这是一个黑客攻击,但至少在这种情况下并不是一个完全丑陋的黑客攻击,因为使用声音来保持 iPhone 唤醒以播放声音具有某种对称性。

之前链接的内容
来自 Apple 关于实现长时间运行的后台任务的指南

播放背景音频
连续播放音频的应用程序(即使应用程序在后台运行时)可以注册为
后台音频应用程序通过包含 UIBackgroundModes 键(使用
值音频)在其 Info.plist 文件中。包含此密钥的应用程序必须
在后台向用户播放声音内容。

你在你的应用程序中实现这个吗?如果没有,那是你的问题。

关于#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:

Playing Background Audio
An app that plays audio continuously (even while the app is running in the background) can register as a
background audio app by including the UIBackgroundModes key (with the
value audio) in its Info.plist file. Apps that include this key must
play audible content to the user while in the background.

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. :)

盗琴音 2024-12-24 01:33:13

在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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文