Get your own website
Demo.swift
ContentView.swift
App.swift
 
import UIKit

class AppDelegate: UIResponder, UIApplicationDelegate {
  var bgCompletionHandler: (() -> Void)?

  func application(_ application: UIApplication,
    handleEventsForBackgroundURLSession identifier: String,
    completionHandler: @escaping () -> Void) {
    bgCompletionHandler = completionHandler
  }
}

// Later, after URLSession finishes all events:
extension BGSession: URLSessionDelegate {
  func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {
    DispatchQueue.main.async {
      (UIApplication.shared.delegate as? AppDelegate)?.bgCompletionHandler?()
      (UIApplication.shared.delegate as? AppDelegate)?.bgCompletionHandler = nil
    }
  }
}

                    
import SwiftUI

struct ContentView: View {
  var body: some View {
    VStack(spacing: 12) {
      Text("Background URLSession completion handler will be invoked when events finish.")
    }
    .padding()
  }
}

                    
import SwiftUI

@main
struct MyApp: App {
  var body: some Scene {
    WindowGroup { ContentView() }
  }
}