amplify-swift icon indicating copy to clipboard operation
amplify-swift copied to clipboard

Add Amplify Storage API for resuming app when background transfer completes

Open palpatim opened this issue 4 years ago • 2 comments

In https://github.com/aws-amplify/docs/issues/2699#issuecomment-733533153, @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 https://github.com/aws-amplify/docs/issues/2699#issuecomment-733533153

palpatim avatar Mar 12 '21 15:03 palpatim

This issue is stale because it has been open for 14 days with no activity. Please, provide an update or it will be automatically closed in 7 days.

github-actions[bot] avatar Jun 19 '21 00:06 github-actions[bot]

PR that will enable to invoke handleEventsForBackgroundURLSession - https://github.com/aws-amplify/amplify-swift/pull/2481

royjit avatar Feb 10 '23 23:02 royjit