Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 通过有限长度任务识别应用程序终止_Ios_Swift_Background - Fatal编程技术网

Ios 通过有限长度任务识别应用程序终止

Ios 通过有限长度任务识别应用程序终止,ios,swift,background,Ios,Swift,Background,为了确定应用程序的终止,我已经执行了有限长度任务,但在一段时间后,应用程序被终止并从开始屏幕启动 func registerBackgroundTask() { backgroundTask = UIApplication.shared.beginBackgroundTask { [weak self] in self?.endBackgroundTask() } assert(backgroundTask != .invalid) } func en

为了确定应用程序的终止,我已经执行了有限长度任务,但在一段时间后,应用程序被终止并从开始屏幕启动

func registerBackgroundTask() {
    backgroundTask = UIApplication.shared.beginBackgroundTask { [weak self] in
        self?.endBackgroundTask()
    }
    assert(backgroundTask != .invalid)
}


func endBackgroundTask() {
    UIApplication.shared.endBackgroundTask(backgroundTask)
    backgroundTask = .invalid
    if backgroundTask != .invalid {
        endBackgroundTask()
    }
}

 func applicationDidEnterBackground(_ application: UIApplication) {
    registerBackgroundTask()
}
执行有限长度任务时不应终止应用程序,也请提供示例,以确定应用程序处于挂起模式时的终止情况

执行有限长度任务时不应终止应用程序

那根本就没有承诺。您的应用程序可能会被终止,原因有很多。beginBackgroundTask是向操作系统请求更多时间来完成用户的短时间运行操作。它不能保证你的请求会被批准

您这样做的具体方式可能偶尔会失败,文档中对此进行了详细说明:

在开始任务之前尽早调用此方法,最好在应用程序实际进入后台之前调用。该方法异步请求应用程序的任务断言。如果在应用程序即将挂起之前不久调用此方法,则系统可能会在授予该任务断言之前挂起应用程序。例如,不要在ApplicationIdentinterBackground:方法结束时调用此方法,并期望应用程序继续运行。如果系统无法授予任务断言,它将调用过期处理程序

您希望包装要请求时间的特定操作。不管你认为你是否要进入后台,你总是要包装这个操作。你不是每次进入后台都会打电话

请提供示例,说明应用程序处于暂停模式时的终止情况

在ApplicationWilenterBackground中,将值写入UserDefaults。在ApplicationIdentiterForeGround中,拔下钥匙。在applicationWillFinishLaunching中,查找该键。如果它在那里,那么你就被重新启动了,所以在某个时候你死在了幕后。这包括重启或电源故障。如果它不在那里,那么这是第一次发射,或者你在前景坠毁。实现这一点的精确方法在很大程度上取决于您计划如何处理这些信息