BackgroundTask iOS 13 Swift with Scenes



我正在尝试使用使用场景(也是新(的新应用程序实现新的iOS 13后台任务功能。 没有很多示例,但我找到的示例没有使用 SceneDelegates,而是所有逻辑都包含在 AppDelegate 中。 这会导致问题,因为进入后台的应用程序的事件现在在场景委托中引发,而不是在应用程序委托中引发。

使用场景时,此方法不会在应用程序委托中触发

func applicationDidEnterBackground(_ application: UIApplication) {
}

此方法在场景委托中触发

func sceneDidEnterBackground(_ scene: UIScene) {
}

我无法在AppDelegate中找到以下方法的相应SceneDelegate方法,其中所有示例都显示了BGTaskScheduler注册任务

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
BGTaskScheduler.shared.register(forTaskWithIdentifier: "UNIQUE_ID", using: DispatchQueue.global()) { task in
refreshMethod(task: task as! BGAppRefreshTask)
}
}

我的问题是 SceneDelegate 中是否有一些事件可以注册任务,如果没有,建议基于场景的应用程序遵循的模式是什么。

AppDelegate.didFinishLaunchingWithOptions在应用程序启动时仍会调用,即使您有 SceneDelegate。因此,应继续在该方法中注册后台任务。

最新更新