gpt4 book ai didi

ios - 将 BGTaskScheduler API 与新的 iOS 14 应用程序结合使用。如何在没有 AppDelegate 的情况下注册任务?

转载 作者:行者123 更新时间:2023-12-04 15:11:47 24 4
gpt4 key购买 nike

我正在使用 BGTaskScheduler API 在我的 iOS 14 应用程序中注册后台任务,该应用程序使用新应用程序作为 @Main而不是 AppDelegate .我以为我们要使用 scenePhase如下所示,以模仿 AppDelegate 中 didFinishLaunching 的先前功能,但以下会导致崩溃:*** Assertion failure in -[BGTaskScheduler _unsafe_registerForTaskWithIdentifier:usingQueue:launchHandler:], BGTaskScheduler.m:185 2020-12-01 12:53:40.645091-0500 newFitnessApp[13487:1952133] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'All launch handlers must be registered before application finishes launching'如何实现BGTaskScheduler?

@main
struct newFitnessAppApp: App {

@Environment(\.scenePhase) var scenePhase

//Launch Count (for requesting reviews and to show tutorial)
//var launchCount = UserDefaults.standard.integer(forKey: TrackerConstants.launchCountKey)
//var testMode = true
//@Environment(\.scenePhase) private var phase
let trackerDataStore = TrackerDataStore(workoutLoader: HealthKitWorkoutLoader())

var body: some Scene {
WindowGroup {
AppRootView().environmentObject(trackerDataStore)
}
.onChange(of: scenePhase) { newScenePhase in
switch newScenePhase {
case .active:
print("App is active")
registerBackgroundTasks()

最佳答案

尝试在init中执行

@main
struct newFitnessAppApp: App {

init() {
registerBackgroundTasks()
}

// ... other code
}

或使用 AppDelegate 适配器在委托(delegate)回调中执行此操作 https://stackoverflow.com/a/62538373/12299030

关于ios - 将 BGTaskScheduler API 与新的 iOS 14 应用程序结合使用。如何在没有 AppDelegate 的情况下注册任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65096278/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com