ios 在后台运行应用程序超过 10 分钟

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9738488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:22:10  来源:igfitidea点击:

Run app for more than 10 minutes in background

iosbackground-process

提问by user968597

I am trying to keep the iOS app in active state for more than 10 mins when it enters in background state.

当它进入后台状态时,我试图将 iOS 应用程序保持在活动状态超过 10 分钟。

How can I implement this.

我该如何实现这一点。

回答by Brian Cooley

See "Background Execution" section of the iPhoneAppProgrammingGuide. In short, your app must be one of these types:

请参阅iPhoneAppProgrammingGuide 的“后台执行”部分。简而言之,您的应用程序必须是以下类型之一:

  • Apps that play audible content to the user while in the background, such as a music player app
  • Apps that keep users informed of their location at all times, such as a navigation app
  • Apps that support Voice over Internet Protocol (VoIP)
  • Newsstand apps that need to download and process new content
  • Apps that receive regular updates from external accessories
  • 在后台向用户播放可听内容的应用程序,例如音乐播放器应用程序
  • 让用户随时了解其位置的应用程序,例如导航应用程序
  • 支持互联网协议语音 (VoIP) 的应用
  • 需要下载和处理新内容的报亭应用
  • 从外部配件接收定期更新的应用程序

And you must add to the Info.plist as follows: Add the UIBackgroundModes key to your Info.plist file and set its value to an array containing one or more of the following strings:

并且您必须按如下方式添加到 Info.plist 中:将 UIBackgroundModes 键添加到您的 Info.plist 文件中,并将其值设置为包含以下一个或多个字符串的数组:

  • audio—The app plays audible content to the user while in the background. (This content includes streaming audio or video content using AirPlay.)
  • location—The app keeps users informed of their location, even while it is running in the background.
  • voip—The app provides the ability for the user to make phone calls using an Internet connection.
  • newsstand-content—The app is aNewsstand app that downloads and processesmagazine or newspaper content in the background.
  • external-accessory—The app works with a hardware accessory that needs to deliver updates on a regular schedule through the External Accessory framework.
  • bluetooth-central—The app works with a Bluetooth accessory that needs to deliver updates on a regular schedule through the CoreBluetooth framework
  • 音频 - 应用程序在后台向用户播放可听内容。(此内容包括使用 AirPlay 流式传输音频或视频内容。)
  • 位置 - 即使在后台运行时,该应用程序也会让用户了解他们的位置。
  • voip - 该应用程序使用户能够使用 Internet 连接拨打电话。
  • newsstand-content——该应用程序是一个在后台下载和处理杂志或报纸内容的报亭应用程序。
  • external-accessory - 该应用程序与需要通过外部附件框架定期提供更新的硬件附件配合使用。
  • bluetooth-central—该应用程序与需要通过 CoreBluetooth 框架定期提供更新的蓝牙附件配合使用

Note that part of the review process will be checking to make sure that your app does what it says it's doing with regard to background processing.

请注意,审核过程的一部分将检查以确保您的应用程序按照它所说的进行后台处理。

回答by ingh.am

Here's what I've done using beginBackgroundTaskWithExpirationHandler.

这是我使用beginBackgroundTaskWithExpirationHandler 所做的

  • Write a method that starts a background task.
  • Inside that background task, run a NSTimerwith a scheduled (non repeating) time that is under 10 minutes. For the purposes of my situation I was using 5 minutes.
  • Once the NStimer's selector fires, end the background task and then instantly call the method that you wrote earlier to start off another background task.
  • If you want to schedule methods to run at specific times, you will have to check for them in the background task.
  • 编写一个启动后台任务的方法。
  • 在该后台任务中,NSTimer以低于 10 分钟的计划(非重复)时间运行 a 。就我的情况而言,我使用了 5 分钟。
  • 一旦NStimer的选择火灾,结束后台任务,然后立即调用您之前写的,开始另一个后台任务的方法。
  • 如果要安排方法在特定时间运行,则必须在后台任务中检查它们。

This solution isn't really ideal and is still power hungry but will do what you want.

这个解决方案并不是很理想,仍然很耗电,但会做你想做的。

Edit: Since iOS7, I suggest you read this excellent post. Note that this article was last updated in 2013 and is probably irrelevant now.

编辑:从 iOS7 开始,我建议你阅读这篇优秀的文章。请注意,本文最后一次更新是在 2013 年,现在可能已经无关紧要。

回答by rismay

This code makes your iOS app run indefinitely in the background. Copy and paste the below methods into a singleton / manager which handles the tasks you need to perform in the background.

此代码使您的 iOS 应用程序在后台无限期运行。将以下方法复制并粘贴到处理您需要在后台执行的任务的单例/管理器中。

// @interface

// Declare Private property
@property (nonatomic) UIBackgroundTaskIdentifier backgroundTask;

//@end
// ...

// Copy into
//@implementation 

- (void)setupBackgrounding {
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(appBackgrounding:)
                                                 name: UIApplicationDidEnterBackgroundNotification
                                               object: nil];
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(appForegrounding:)
                                                 name: UIApplicationWillEnterForegroundNotification
                                               object: nil];
}

- (void)appBackgrounding: (NSNotification *)notification {
    [self keepAlive];
}

- (void) keepAlive {
    self.backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
        self.backgroundTask = UIBackgroundTaskInvalid;
        [self keepAlive];
    }];
}

- (void)appForegrounding: (NSNotification *)notification {
    if (self.backgroundTask != UIBackgroundTaskInvalid) {
        [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
        self.backgroundTask = UIBackgroundTaskInvalid;
    }
}

回答by CJ Foley

Only certain types of apps are allowed to run in the background. See the "Implementing Long-Running Background Tasks" section of this guide.

只允许某些类型的应用程序在后台运行。请参阅本指南的“实施长时间运行的后台任务”部分。

If you aren't requesting permissions to do background processing you can use UIApplication's beginBackgroundTaskWithExpirationHandlerbut you cannot get extra time.

如果您不请求权限进行后台处理,您可以使用 UIApplication 的beginBackgroundTaskWithExpirationHandler但您无法获得额外的时间。

回答by fbernardo

You can't. Unless your app uses audio, voip or gps. What you can do is notify the user (via local notifications) that the time is almost up and ask him to open/close the app.

你不能。除非您的应用使用音频、voip 或 GPS。您可以做的是通知用户(通过本地通知)时间快到了,并要求他打开/关闭应用程序。

Also if you just need to notify the user, you can use push notifications.

此外,如果您只需要通知用户,您可以使用推送通知。

回答by Maksim Kniazev

https://github.com/yarodevuci/backgroundTaskCheck my code here I am using audio player that plays blank wav file Works perfectly on IOS 8 Battery usage around 10% in 24 hour period How to use:

https://github.com/yarodevuci/backgroundTask在这里检查我的代码 我正在使用播放空白 wav 文件的音频播放器 在 IOS 8 上完美运行 24 小时内电池使用率约为 10% 如何使用:

var backgroundTask = BackgroundTask()
backgroundTask.startBackgroundTask() //Starts playing blank audio file. You can run NSTimer() or whatever you need and it will continue executing in the background.

backgroundTask.stopBackgroundTask() //Stops the task

Warning: Apple will reject this if you try to submit it!

警告:如果您尝试提交,Apple 将拒绝此请求!

回答by DàChún

If your App type is not one of VOIP/Audio/Location....(check Background Modes),

如果您的应用程序类型不是 VOIP/音频/位置.....(检查背景模式),

or you don't want to specify your App as a background App, you can implement beginBackgroundTaskWithName:expirationHandleror beginBackgroundTaskWithExpirationHandlerto ask for more time to run your process in background. You can find the detailed description here

或者您不想将您的应用程序指定为后台应用程序,您可以实现beginBackgroundTaskWithName:expirationHandlerbeginBackgroundTaskWithExpirationHandler以请求更多时间在后台运行您的进程。您可以在此处找到详细说明

Apps moving to the background are expected to put themselves into a quiescent state as quickly as possible so that they can be suspended by the system. If your app is in the middle of a task and needs a little extra time to complete that task, it can call the beginBackgroundTaskWithName:expirationHandler: or beginBackgroundTaskWithExpirationHandler: method of the UIApplication object to request some additional execution time. Calling either of these methods delays the suspension of your app temporarily, giving it a little extra time to finish its work. Upon completion of that work, your app must call the endBackgroundTask: method to let the system know that it is finished and can be suspended.

Each call to the beginBackgroundTaskWithName:expirationHandler: or beginBackgroundTaskWithExpirationHandler: method generates a unique token to associate with the corresponding task. When your app completes a task, it must call the endBackgroundTask: method with the corresponding token to let the system know that the task is complete. Failure to call the endBackgroundTask: method for a background task will result in the termination of your app. If you provided an expiration handler when starting the task, the system calls that handler and gives you one last chance to end the task and avoid termination.

移动到后台的应用程序应尽快将自己置于静止状态,以便系统可以暂停它们。如果您的应用程序处于任务中间并且需要一点额外时间来完成该任务,它可以调用 UIApplication 对象的 beginBackgroundTaskWithName:expirationHandler: 或 beginBackgroundTaskWithExpirationHandler: 方法来请求一些额外的执行时间。调用这些方法中的任何一个都会暂时延迟您的应用程序的暂停,从而给它一些额外的时间来完成其工作。完成这项工作后,您的应用程序必须调用 endBackgroundTask: 方法让系统知道它已完成并可暂停。

每次调用 beginBackgroundTaskWithName:expirationHandler: 或 beginBackgroundTaskWithExpirationHandler: 方法都会生成一个与相应任务相关联的唯一标记。当你的应用程序完成的任务时,它必须调用endBackgroundTask:方法与相应的记号,让系统知道,任务就完成了。未能为后台任务调用 endBackgroundTask: 方法将导致您的应用程序终止。如果您在启动任务时提供了过期处理程序,系统将调用该处理程序,并为您提供最后一次结束任务并避免终止的机会。