When the user kill my app, I need to make an API call to notify my backend.
The solution I found is to blocks the main thread with a semaphore so the HTTP call can be made before app destruction:
func applicationWillTerminate(_ application: UIApplication) {
let semaphore = DispatchSemaphore(value: 0)
// Some async API call
URLSession.shared.dataTask(with: request) { _, _, _ in
semaphore.signal()
}.resume()
_ = semaphore.wait(timeout: .now() + .seconds(2))
}
It looks like a bit “hacky”
Is there a better way to achieve this ?
Could this cause an App Store rejection ?