title: ios 9, Share Extension, Background Upload Session, Swift
body: <p>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.</p>
  
  <pre><code>    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
  </code></pre>
  
  <p>We get information back from this upload that is needed for successive endpoint Posts.</p>
  
  <p>Once this is done, the container app is being called with</p>
  
  <pre><code>func application(application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: () -&gt; Void) 
  </code></pre>
  
  <p>But this is being called sometimes before the upload's <code>Success</code> 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.</p>
  
  <p>Questions:</p>
  
  <p>1) How to ensure that the return information in the Share Extension will be available before the app call?</p>
  
  <p>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.</p>
  
  <p>3) Is there a way to figure out if the background session succeeded in the app's event handler routine?</p>
  
  <p>4) Is there a way to get the result data in the event handler routine?</p>
  
  <pre><code>let configuration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("\SomeName")
  let session = NSURLSession(configuration: configuration, delegate: self, delegateQueue: NSOperationQueue.mainQueue())
  </code></pre>
  
  <p>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?</p>