背景任务 iOS 13 Swift 与场景

Chr*_*ger 6 swift ios13

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

使用场景时,此方法不会在 AppDelegate 中触发

func applicationDidEnterBackground(_ application: UIApplication) {

}
Run Code Online (Sandbox Code Playgroud)

此方法确实在 SceneDelegate 中触发

func sceneDidEnterBackground(_ scene: UIScene) {

}
Run Code Online (Sandbox Code Playgroud)

我无法在 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)
        }

}
Run Code Online (Sandbox Code Playgroud)

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

sob*_*bri 0

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