简体   繁体   English

防止iOS几分钟后杀死App

[英]Prevent iOS from killing App after a few minutes

I'd like to prevent iOS from killing my app after a few minutes.我想阻止 iOS 在几分钟后杀死我的应用程序。 I've read this thread here on SO: Prevent iOS from killing my app after 3 minutes .我已经在 SO 上阅读了这个线程: 防止 iOS 在 3 分钟后杀死我的应用程序 It says that if I have no backgroundtasks longer than 3 minutes my app wont be killed.它说如果我没有超过 3 分钟的后台任务,我的应用程序不会被杀死。 Can someone verify that this is true?有人可以验证这是真的吗? Because my background-task is not running longer than 3 minutes and even though my app gets killed after this time.因为我的后台任务运行时间不超过 3 分钟,即使我的应用程序在此时间之后被杀死。 My background-task is a timer that updates a widget.我的后台任务是一个更新小部件的计时器。 Heres some code:继承人一些代码:

self.backgroundTask = UIApplication.shared.beginBackgroundTask { [weak self] in
        self?.endBackgroundTask()
//endBackGroundTask looks like this
UIApplication.shared.endBackgroundTask(self.backgroundTask)
    self.backgroundTask = UIBackgroundTaskInvalid
//
    }
self.timer = Timer.scheduledTimer(timeInterval: 1, target: self,   selector: (#selector(self.updateTimer)), userInfo: nil, repeats: true)

. .

// at the beginning of the class
var backgroundTask: UIBackgroundTaskIdentifier = UIBackgroundTaskInvalid

. .

// in viewWillDisappear
self.timer.invalidate()
    if self.backgroundTask != UIBackgroundTaskInvalid {
        self.endBackgroundTask()

    }

You need to structure your app so that it doesn't require continual execution in the background.您需要构建您的应用程序,使其不需要在后台持续执行。 As I understand it, your app shows a count down timer and can show the same count down timer in a Today Widget.据我了解,您的应用程序显示倒计时计时器,并且可以在“今日”小部件中显示相同的倒计时计时器。 The approach I would use is follows:我将使用的方法如下:

  • Store the "end date" for the timer in user defaults to share with your widget将计时器的“结束日期”存储在用户默认值中以与您的小部件共享
  • When your app is in the foreground, use a Timer to periodically update your UI当您的应用程序在前台时,使用Timer定期更新您的 UI
  • When your Widget is being displayed use a Timer in the widget to periodically update its UI当您的小部件显示时,在小部件中使用Timer来定期更新其 UI
  • When your app moves to the background, schedule a local notification for the expiration time当您的应用程序移至后台时,为到期时间安排本地通知
  • When your app moves back to the foreground, you can cancel that scheduled notification if it hasn't yet fired.当您的应用返回前台时,如果该通知尚未触发,您可以取消该通知。
  • Support app restoration for those cases where your app is legitimately terminated (eg due to memory pressure or being suspended for a long period)在您的应用程序被合法终止的情况下支持应用程序恢复(例如由于内存压力或长时间暂停)

If you do this then you never need to call beginBackgroundTask .如果你这样做,那么你永远不需要调用beginBackgroundTask If you do call beginBackgroundTask and don't call endBackgroundTask within 3 minutes of entering the background, then your app will be terminated, even if you aren't using any CPU.如果您确实调用了beginBackgroundTask并且在进入后台的 3 分钟内没有调用endBackgroundTask ,那么您的应用程序将被终止,即使您没有使用任何 CPU。

Short answer: You can't run a background task for longer than 3 minutes unless you are a turn-by-turn navigation app or an audio player.简短回答:除非您是逐向导航应用程序或音频播放器,否则您不能运行超过 3 分钟的后台任务。 Apple doesn't allow it by design. Apple 设计上不允许这样做。

Your background task is a timer that is running longer than 3 minutes.您的后台任务是一个运行时间超过 3 分钟的计时器。 So your app is correctly being killed.所以你的应用程序被正确地杀死了。 Consider it confirmed as that is Apple's design.考虑确认这是苹果的设计。

It's not what your timer is executing that is killing the app, it's the timer itself.杀死应用程序的不是您的计时器正在执行的操作,而是计时器本身。

You can read up on Apple's Documentation for more information.您可以阅读Apple 的文档以获取更多信息。

Always try to avoid doing any background work unless doing so improves the overall user experience.始终尽量避免做任何后台工作,除非这样做可以改善整体用户体验。 An app might move to the background because the user launched a different app or because the user locked the device and is not using it right now.应用程序可能会移至后台,因为用户启动了不同的应用程序,或者因为用户锁定了设备并且现在没有使用它。 In both situations, the user is signaling that your app does not need to be doing any meaningful work right now.在这两种情况下,用户都在表示您的应用程序现在不需要做任何有意义的工作。 Continuing to run in such conditions will only drain the device's battery and might lead the user to force quit your app altogether.在这种情况下继续运行只会耗尽设备的电池,并可能导致用户完全强制退出您的应用程序。 So be mindful about the work you do in the background and avoid it when you can.因此,请注意您在后台所做的工作,并尽可能避免它。

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

相关问题 防止iOS在3分钟后杀死我的应用 - Prevent iOS from killing my app after 3 minutes 有没有办法阻止ios杀死我的应用程序? - Is there any way to prevent the ios from killing my app? 杀毒App(iOS)后Facebook AccessToken无 - Facebook AccessToken nil after killing App (iOS) 从后台杀死应用程序后,应该在ios中运行gps跟踪应用程序(目标c) - After killing the app from the background, app should run gps tracking application in ios (objective c) 从后台终止应用程序后,UILocalnotification无法正常工作 - UILocalnotification not working after killing the app from background 如何防止主线程ios杀死后台线程 - how to prevent main thread ios from killing background thread 前台应用程序处于什么状态但几分钟后显示为非活动状态? - What is the state of App in foreground but appears inactive after few minutes? iOS 5-Coredata Sqlite DB终止应用程序后丢失数据 - iOS 5 - Coredata Sqlite DB losing data after killing app MPMusicPlayerController.play() 在杀死 iOS 音乐应用程序后不起作用 - MPMusicPlayerController.play() not working after killing iOS music app 几轮后ios Puzzle App崩溃 - ios puzzle app crashes after a few rounds
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM