Skip to content

Add Amplify Storage API for resuming app when background transfer completes #1102

Closed
@palpatim

Description

@palpatim

In aws-amplify/docs#2699 (comment), @ptwobrussell noted that we do not provide documentation or an API for resuming an app when a background transfer completes.

We should investigate the various ways our supported iOS versions notify an app that the session has completed, and ensure we provide clear documentation and necessary Amplify Storage APIs for re-launching an app when a transfer completes.


Just to circle back on this, I have figured out the solution.

1 - In terms of the code itself, you'll just drop in something to this effect into your AppDelegate.swift file. What makes it a bit non-obvious is that you're importing AWSS3 directly and there are no references to Amplify.

import Foundation
import UIKit
import AWSS3
import os.log


// Needed to handle completion of background sessions for file uploads
// If you're working on a new project with SwiftUI and iOS13+ you'll have to go out of your way to wire up `AppDelegate` through
// the @UIApplicationDelegateAdaptor property wrapper
class AppDelegate: NSObject, UIApplicationDelegate {
       
    func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {
        os_log("handleEventsForBackgroundUrlSession: %{public}@", log: OSLog.fileupload, type: .debug, identifier)
        AWSS3TransferUtility.interceptApplication(application, handleEventsForBackgroundURLSession: identifier, completionHandler: completionHandler)
    }
    
}

2 - Here's the part that was really at the root of most of my troubles: in iOS 13+, it's possible that your iOS Settings can be inadvertently configured so that iOS never resumes/relaunches your app, which can be incredibly confusing and send you on lots of wild goose chases if you don't already know about this. See https://developer.apple.com/forums/thread/124265?answerId=388440022#388440022

This was a fairly painful and confusing debugging process to get to to the bottom of this. Given how common a use case it is to upload large files, I hope this is helpful to someone else.

Originally posted by @ptwobrussell in aws-amplify/docs#2699 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature-requestRequest a new featurestorageIssues related to the Storage category

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions