在iOS 5上屏幕锁定时不会发生NSTimer事件 [英] NSTimer events do no occur when screen locked on iOS 5

查看:117
本文介绍了在iOS 5上屏幕锁定时不会发生NSTimer事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



自iOS 5以来我的应用程序似乎遇到了如果用户锁定屏幕我的NSTimer在锁定屏幕中大约20秒后停止发射的问题。如果用户锁定屏幕,这使得我的应用程序在每次定时器触发时都会发出声音。



这只发生在我将设备更新到iOS 5之后。 / p>

我的问题是1.是否有任何解决方法可以在更新的设备上使用相同的功能或2.如果您的应用程序没有,建议的方法是不接收差评在锁定时运行。

解决方案

当用户锁定屏幕时,应用程序进入非活动状态。这与将应用程序输入后台类似但不完全相同。 Apple的文档声明该应用程序正在前台运行但当前没有接收事件。(它可能正在执行其他代码。)



但是,在进入非活动状态后表示iPhone确实会完全进入睡眠状态,除非正在运行以保持其活动状态......等待NSTimer发射并不符合此标准。 上一个问题中有讨论。一个解决方案,它会出现,是在你的计时器开启时​​播放你的声音,并在此期间,连续播放无声声音,以防止iPhone进入睡眠状态。一个hack,但至少在这种情况下不是一个完全丑陋的黑客,因为使用声音以保持iPhone唤醒以播放声音具有一种对称性。



以前链接的内容

来自 Apple关于实施长时间运行后台任务的指南


播放背景音频

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


您是否正在实施此功能应用程序吗?如果没有,那就是你的问题。



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

解决方案

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

这篇关于在iOS 5上屏幕锁定时不会发生NSTimer事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆