background task is not working on watchos8

reference : https://wjwickham.com/posts/refreshing-data-in-the-background-on-watchOS/

on init function , which mean, for every 5 min it should come to handle function in Extension Delegate.swift but it won't come to handle any anything else that I should do ?

let preferredDate = Date().addingTimeInterval(60 * 5)// 5min later
        preferredDate, userInfo: nil) { (error) in
        WKExtension.shared().scheduleBackgroundRefresh(withPreferredDate: preferredDate, userInfo: nil) { (error: Error?) in
            guard error == nil else {
                    print("Couldn't schedule background refresh.")
                    return
                }

                print("Scheduled next background update task for: \(preferredDate)")

        }

ExtensionDelegate.swift

//
//  ExtensionDelegate.swift
//  Coffee Tracker WatchKit Extension
//
//  Created by pedro jung on 2022/06/15.
//  Copyright © 2022 Apple. All rights reserved.
//

import Foundation
import WatchKit

class ExtensionDelegate: NSObject,ObservableObject, WKExtensionDelegate {
    
    func applicationDidFinishLaunching() {
        print("applicationDidFinishLaunching")
        // Perform any final initialization of your application.
    }

    func applicationDidBecomeActive() {
        
            print("applicationDidBecomeActive")
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillResignActive() {
        
            print("applicationWillResignActive")
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, etc.
    }
    func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {
        for task in backgroundTasks {

            switch task {
            case let backgroundTask as WKApplicationRefreshBackgroundTask:
                BackgroundService.shared.updateContent()
                backgroundTask.setTaskCompletedWithSnapshot(false)
                
            case let urlSessionTask as WKURLSessionRefreshBackgroundTask:
                BackgroundService.shared.handleDownload(urlSessionTask)
                
            default:
                task.setTaskCompletedWithSnapshot(false)
            }
        }
    }

}

I also check other way after checking reply of below link https://developer.apple.com/forums/thread/661892 but with no luck ...

import SwiftUI
import WatchKit
@main
struct CoffeeTrackerApp: App {
    
    @Environment(\.scenePhase) var scenePhase
    @WKExtensionDelegateAdaptor(ExtensionDelegate.self) var delegate
    @SceneBuilder var body: some Scene {
        WindowGroup {
            NavigationView {
                ContentView()
            }
            .onChange(of: scenePhase) { phase in
                        switch phase {
                            case .active:
                                print("\(#function) REPORTS - App change of scenePhase to ACTIVE")
                            case .inactive:
                                print("\(#function) REPORTS - App change of scenePhase Inactive")
                            case .background:
                            let preferredDate = Date().addingTimeInterval(60 * 1)// One hour later
                            
                    //        delegate.shared().scheduleBackgroundRefresh(withPreferredDate: preferredDate, userInfo: nil) { (error) in
                            WKExtension.shared().scheduleBackgroundRefresh(withPreferredDate: preferredDate, userInfo: nil) { (error: Error?) in
                                guard error == nil else {
                                        print("Couldn't schedule background refresh.")
                                        return
                                    }

                                    print("Scheduled next background update task for: \(preferredDate)")

                            }
                            delegate.test()
                            default:
                                print("\(#function) REPORTS - App change of scenePhase Default")
                        }
            }
        }
    }
}
Post not yet marked as solved Up vote post of kotran Down vote post of kotran
1.4k views

Replies

Your app gets at most 4 background app refresh opportunities per hour, if you have a complication on the active watch face. A few WWDC videos that you might find useful: