Revolutionising User Experience with iOS Background Assets
Introduction
The Background Assets framework is a groundbreaking solution designed to eliminate the frustrating wait times users often experience during app launches, particularly when additional content is required upon the first launch. This framework ensures that the app’s content is preloaded, guaranteeing a seamless and instant user experience. Let’s delve into the core components and functionalities of this innovative iOS framework.
BackgroundAsset Extension:
- Operates outside the app’s life cycle, scheduling downloads in the background.
- Invoked during app installation, updates, or periodically
- periodical invoking intervals based on user app usage patterns.
- Essential content can’t be scheduled during periodic events
- Short-lived; if downloads aren’t scheduled promptly, the system may terminate the extension.
- Contains
BADownloaderExtension
protocol object for scheduling and managing downloaded files.
Functions:
downloads(for request: BAContentRequest, manifestURL: URL, extensionInfo: BAAppExtensionInfo) -> Set<BADownload>
:
- Invoked by the system to request downloads from the extension.
- Uses manifest files to determine assets for download.It is encouraged to use manifest file to determine what assets need to be downloaded
- Schedules downloads using
BAURLDownload
objects.
2. backgroundDownload(_ finishedDownload: BADownload, finishedWithFileURL fileURL: URL)
:
- Called when a download is complete.
- Downloaded file available at
fileURL
. - Advised to store files in
Library/Caches
for efficient storage management.
3. backgroundDownload(_ failedDownload: BADownload, failedWithError error: Error)
:
- Called when an asset fails to download.
- Allows rescheduling of downloads with
BADownloadManager
if necessary.
Downloading asset inside main Target:
- If the assets are not downloaded during first launch, we can cancel/reschedule or can promote background to foreground from the app.
- By using `BADownloadManager`,we can schedule download in foreground and background
- The downloading status are updated in `BADownloadManagerDelegate` object which is assigned to BADownloadManager’s delegate
Note:The delegate receives messages for all downloads that have been scheduled by either the extension or your app. refer here
- Use BADownloadManager to schedule and cancel asset downloads, monitor their progress, and access the queue of pending downloads
- We can schedule download in foreground and background
- Access the framework’s singleton through the shared property(don’t create object)
- Manager is shared between extension and the app
- Since the download manager is a shared resource, to avoid race conditions by using the withExclusiveControl(:) and withExclusiveControl(beforeDate::) methods to establish exclusive control before scheduling or manipulating downloads
- Scheduling Foreground download using manager
- startForegroundDownload(_ download: BADownload) function schedules download in foreground
- Foreground downloads have increased priority and start immediately
Note: Performing a foreground download is not available within the extension.It can only be initiated from the app.
- Scheduling background download using manager
- scheduleDownload(_:) function schedules an asset download to execute in the background at a non-specific time in the future
- cancel(_:) function used to cancel download
- fetchCurrentDownloads(completionHandler:) function returns all in-progress and enqueued downloads
- Promoting background download to foreground
- First fetch download objects which you want to promote
- Then we can use startForegroundDownload(_ download: BADownload) function to promote downloads
Note : If the download was started backgrounded, it will first be paused, then resumed in the foreground without requiring any content that was already downloaded
Essential and Non-Essential Downloads:
- Essential downloads are crucial for fundamental app operations. Essential downloads take priority over non-essential ones.
- Non-essential downloads enhance the overall user experience.
- Non-essential downloads occur in the background without hindering app launch.
- Essential downloads will be started by the system while your app is installing/updating, and the user cannot launch the app until they complete or fail.
- Essential downloads seamlessly integrate into iOS Home Screen, macOS Launchpad, and the App Store, presenting to users as if the app is still being downloaded from the App Store.
Note: Users can cancel or pause essential downloads. Server support for HTTP ranges is essential for resume functionality.
App Store Settings:
- Users can disable in-app content in App Store settings (settings->App Store->In App Content. It will not restrict the downloads scheduled from app).
- Essential assets are important but not mandatory for app launch, necessitating consideration for scenarios where these assets are not yet on the device when the app starts)