[英]Running the code when application is in the background or device is locked
I have a piece of code wherein I make a server call and based on the response I play a sound. 我有一段代码,其中我进行服务器调用,并根据响应播放声音。 Now, this does not work when my application is in background or my devicei is locked. 现在,当我的应用程序在后台或我的设备已被锁定时,这将不起作用。
Is there any way we can execute this piece of code (Server call and response handling) even if app is in background or device is locked? 即使应用程序在后台或设备被锁定,我们是否可以执行这段代码(服务器调用和响应处理)?
There is no general solution, which is by design. 没有一般的解决方案,这是设计使然。 (Apple does not want you to have a potentially CPU- and power-intensive process running in the background and degrading user experience.) (Apple不希望您在后台运行可能会占用大量CPU和电源的进程,从而降低用户体验。)
There are a few limited-case options available: 有一些限制情况的选项:
-[UIApplication beginBackgroundTaskWithExpirationHandler:]
from within the appropriate UIApplicationDelegate
methods to register such a task. 在适当的UIApplicationDelegate
方法中使用-[UIApplication beginBackgroundTaskWithExpirationHandler:]
来注册此类任务。 Note that you have a limited (but appreciable) amount of time to finish your task, and I don't think you can play media in this mode. 请注意,您完成工作的时间有限(但可观),而且我认为您不能以这种模式播放媒体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.