简体   繁体   English

当应用程序在后台或设备被锁定时运行代码

[英]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: 有一些限制情况的选项:

  1. You said you want to play a sound. 您说过要播放声音。 If by this you mean "play music" or some such streaming, there is an audio background task that your application can register to perform. 如果您是说“播放音乐”或某种类似的流媒体,那么您的应用程序可以注册执行音频背景任务 Note that you must actually be streaming audio; 请注意,您实际上必须在流音频。 Apple rightfully frowns upon apps that try to use this approach to circumvent the general-case prohibition and will reject your App Store submission accordingly. Apple正确地拒绝尝试使用此方法来绕过一般情况下的禁止的应用程序,并会相应拒绝您的App Store提交。
  2. You can invert your scenario and have the server send a push notification through the Apple Push Notification service . 您可以反转方案,并让服务器通过Apple Push Notification Service发送推送通知 Depending on the user's settings, an alert, badge, or sound can result. 根据用户的设置,可能会产生警报,标志或声音 This might be the best fit for you if you aren't streaming audio. 如果您不流音频,这可能是最适合您的选择。
  3. If what you are really intending to do is, say, finish an upload or download (and the sound is a completion notification), you can request some additional time to finish that task after the app is nominally backgrounded or the device locked. 例如,如果您真正打算做的是完成上传或下载(并且声音是完成通知),则可以在应用程序名义上后台运行或设备锁定后,请求一些额外的时间来完成该任务。 Use -[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.

相关问题 在后台或设备锁定时,iOS应用程序会使用电池吗? - Does an iOS app use battery when in the background or when the device is locked? 当应用程序在后台且设备被锁定时处理推送通知 - Handling push notification when app is in background and device is locked 在锁定的android或ios中收到后台通知时,是否可以启动应用程序? - Is it possible to start an application when a background notification is received in a locked android or ios? 当iPhone锁定/解锁且应用程序处于后台时如何恢复音频 - How to resume audio when iphone is locked/unlocked and the application was in background 允许应用程序在 iPhone 被锁定时继续运行 - Allow application continue running when iPhone gets locked 应用程序在后台运行时运行CLLocation - Running CLLocation when Application is in background 设备锁定时警报不起作用 - Alarm not working when device is locked NSTimer在设备锁定时到期 - NSTimer expires when the device is locked 应用程序在后台时更改设备语言 - Changing the device language when application is in background 在设备上安装时如何将应用程序发送到后台? - How send application to background when install on device?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM