简体   繁体   English

在锁定iOS设备的同时保持应用程序运行?

[英]Keep app running while iOS device locked?

I have an app that makes heavy use of video out. 我有一个可以大量使用视频的应用程序。 In a typical use-case, I'll have an iPad connected to an external monitor. 在一个典型的用例中,我将iPad连接到外接显示器。 I just want the external monitor on; 我只希望外接显示器打开; the iPad display does not need to stay on. iPad显示屏不需要一直保持打开状态。

The ideal case would be for someone to connect to an external monitor, then lock their iPad. 理想的情况是某人连接到外部显示器,然后锁定他们的iPad。 But that pauses my app. 但这暂停了我的应用程序。 (Currently, I'm calling setIdleTimerDisabled to keep the iPad from locking up and pausing my app.) (当前,我正在调用setIdleTimerDisabled以防止iPad锁定并暂停我的应用程序。)

I'd like to give the user the option of locking the iPad, but still having my app running and sending images to video out. 我想为用户提供锁定iPad的选项,但仍然可以让我的应用程序运行并向视频发送图像。 (Note: I'm not talking about keeping my app running when it's not in the foreground. I just want to keep it running while it's in the foreground, but the device is locked.) (注意:我并不是在说我的应用程序不在前台时要保持运行。我只想在应用程序在前台时保持运行,但设备已锁定。)

Is this possible? 这可能吗?

I would say no, it is not possible. 我会说不,这是不可能的。 Here's why: 原因如下:

The docs read: 文档阅读:

Pressing the Sleep/Wake button is another type of interruption that causes your app to be deactivated temporarily. 按下“睡眠/唤醒”按钮是另一种类型的中断,会导致您的应用暂时停用。 When the user presses this button, the system disables touch events, moves the app to the background but sets the value of the app's applicationState property to UIApplicationStateInactive (as opposed to UIApplicationStateBackground ), and finally locks the screen. 当用户按下此按钮时,系统禁用触摸事件,将应用程序移至后台,但将应用程序的applicationState属性的值设置为UIApplicationStateInactive (与UIApplicationStateBackground相对),最后锁定屏幕。

Something interesting to note in the docs above is that a bit further down under "What to do when an interruption occurs" Apple recommends that you stop doing certain tasks. 在上面的文档中需要注意的有趣一点是,在“发生中断时该怎么办”下,Apple建议您停止执行某些任务。

In response to this change, your app should do the following in its applicationWillResignActive: method: 为响应此更改,您的应用程序应在其applicationWillResignActive:方法中执行以下操作:

  • Stop timers and other periodic tasks. 停止计时器和其他定期任务。
  • Stop any running metadata queries. 停止所有正在运行的元数据查询。
  • Do not initiate any new tasks. 不要启动任何新任务。
  • Pause movie playback (except when playing back over AirPlay). 暂停电影播放(通过AirPlay播放时除外)。
  • Enter into a pause state if your app is a game. 如果您的应用是游戏,请进入暂停状态。
  • Throttle back OpenGL ES frame rates. 节流返回OpenGL ES帧速率。
  • Suspend any dispatch queues or operation queues executing non-critical code. 挂起执行非关键代码的所有调度队列或操作队列。 (You can continue processing network requests and other time-sensitive background tasks while inactive.) (您可以在不活动时继续处理网络请求和其他对时间敏感的后台任务。)

This tells me that Apple doesn't want or expect your app to be doing much of anything in this state, other than preparing to be fully backgrounded. 这告诉我,苹果不希望或期望您的应用在这种状态下能做很多事情,除了准备充分使用背景知识外。

On a related note here's a thread that shows how to determine whether you've hit the Sleep/Wake button or not: 在相关说明中,以下是一个线程,该线程显示如何确定您是否已按下“睡眠/唤醒”按钮:

Is it possible to distinguish between locking the device and sending an app to background? 是否可以区分锁定设备和将应用发送到后台?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM