2

We are setting up a Share Extension using Swift. Once the user posts we take the file URL information and do a multi-part upload to a server in the background using Almofire.

    let configuration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("SomeName")
    configuration.sharedContainerIdentifier = "SomeValidShare"
    manager = Alamofire.Manager(configuration: configuration)

    let authHeaderString = authHeader();        
    manager?.upload(.POST, url, headers: authHeaderString, multipartFormData: {
            multipartFormData in multipartFormData.appendBodyPart(fileURL: videoUrl, name: "file")
        },
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .Success(let upload, _, _):

    etc

We get information back from this upload that is needed for successive endpoint Posts.

Once this is done, the container app is being called with

func application(application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: () -> Void) 

But this is being called sometimes before the upload's Success is being run in the share extension and we don't have access to the JSON returned to make more calls from the container app.

Questions:

1) How to ensure that the return information in the Share Extension will be available before the app call?

2) Is there any way to have the app get session / task delegates from this background session when the user brings the app up? We have tried to get the session, but we get a console error that this session already exists and the delegates are not called.

3) Is there a way to figure out if the background session succeeded in the app's event handler routine?

4) Is there a way to get the result data in the event handler routine?

let configuration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("\SomeName")
let session = NSURLSession(configuration: configuration, delegate: self, delegateQueue: NSOperationQueue.mainQueue())

So what is the recommended way to make sure that the result of the share extension's background upload is obtained and then can be transfered to the container app via shared user defaults?

Tomasz Stanczak
  • 12,796
  • 1
  • 30
  • 32
ort11
  • 3,359
  • 4
  • 36
  • 69

0 Answers0