Skip to main content

B. Handling Background Tasks

The Entrupy iOS SDK may perform network operations, such as uploading images, that can continue even if the application is sent to the background. To ensure these tasks complete successfully and the SDK can process their results, it's important to correctly handle background URL session events in your AppDelegate.

1. Intercepting Background URL Session Events

You must call the SDK's interceptApplication method from your AppDelegate's application(_:handleEventsForBackgroundURLSession:completionHandler:) delegate method.

This allows the Entrupy SDK to:

  • Identify and manage its own background network tasks.
  • Process the results of uploads or downloads that completed while the app was suspended.
  • Ensure that any necessary post-transfer handling is completed, waking the app in the background if needed.

Implementation in AppDelegate.swift:

import UIKit
import EntrupySDK // Ensure EntrupySDK is imported

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

// ... other AppDelegate methods ...

func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {
// Pass the event to the Entrupy SDK
// The SDK will check if the identifier belongs to one of its sessions
// and handle it accordingly. It will call the completionHandler when done.
let handledByEntrupy = EntrupyApp.sharedInstance().interceptApplication(
application,
handleEventsForBackgroundURLSession: identifier,
completionHandler: completionHandler
)

// If the identifier was not handled by Entrupy SDK, and you have other
// background tasks, you might handle them here. Otherwise, if Entrupy
// didn't handle it, you might need to call the completionHandler yourself
// if no other part of your app will.
// However, the EntrupyApp.sharedInstance().interceptApplication method,
// as per its typical design, should invoke the completionHandler
// regardless of whether it handled the event or not for simplicity in most cases,
// or return a boolean indicating if it *did* handle it, allowing you to then call it.
// The provided snippet from legacy docs implies it might call it. For robustness, check SDK specifics.

// Assuming EntrupyApp.sharedInstance().interceptApplication ALWAYS calls completionHandler:
// No further action required here if that's the case.

// If EntrupyApp.sharedInstance().interceptApplication returns a Bool and only calls
// completionHandler if it *handled* the event, you might do:
// if !handledByEntrupy {
// completionHandler() // Call if not handled by Entrupy and you have no other handlers
// }
}

// ... other AppDelegate methods ...
}
Note

The exact signature and return type of interceptApplication should be confirmed with the SDK's current API documentation or header files. The example above is based on common patterns and the information in sdk-v1-legacy.md (Section 10), which showed EntrupyApp.sharedInstance().interceptApplication(...) without a return value, implying it handles the completionHandler call internally.

Failure to correctly implement this method can lead to issues with authentications not completing if the app is backgrounded during image uploads.

This setup ensures that the SDK can reliably manage its network activity, contributing to a more robust authentication experience for your users.