title: How to Upload video files in background?
body: <p>I am doing a app in which i need to upload videos taken from iPhone Camera and upload it to a server. When the app is in foreground the video gets uploaded but i don't know how to do it in background when the app is inactive. I used <code>AFNetworking</code> to do the multipart data upload. Here is the code i tried </p>
  
  <pre><code>var task:NSURLSessionUploadTask!
  let FILEPICKER_BASE_URL = "My server Url"
  var isUploading : Bool = false
  var videoPath : String!
  func upload(dictMain: NSMutableDictionary)
  {
      isUploading = true
      let uuid =  NSUUID().UUIDString + "-" + (getUserId().description)
      let request = AFHTTPRequestSerializer().multipartFormRequestWithMethod("POST", URLString: FILEPICKER_BASE_URL, parameters: nil, constructingBodyWithBlock: { (fromData) in
  
          do {
              try fromData.appendPartWithFileURL(NSURL(fileURLWithPath: videoPath) , name: "fileUpload", fileName: uuid, mimeType: "video/quicktime")
          }catch{
              print(error)
          }
          }, error: nil)
  
      let manager:AFURLSessionManager = AFURLSessionManager(sessionConfiguration: NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("Your video is uploading"));
  
      task = manager.uploadTaskWithStreamedRequest(request, progress: nil , completionHandler: { (response, responseObject, error) in
              NSLog("Resposne Object: \(responseObject)")
              post(dictMain,response: responseObject!)
      })
      task.resume()    
  }
  </code></pre>
  
  <p>Also I don't know how to know my upload progress. I tried to use the following block in the progress parameter </p>
  
  <pre><code> { (progress) in
      print("\((progress))")
   }
  </code></pre>
  
  <p>But it does not work the complier shows error 
  <code>Cannot convert value of type '(_) -&gt; _' to expected argument type 'AutoreleasingUnsafeMutablePointer&lt;NSProgress?&gt;' (aka 'AutoreleasingUnsafeMutablePointer&lt;Optional&lt;NSProgress&gt;&gt;')</code></p>
  
  <p>Could any one share a snippet that really works. As i googled there are very very few basic tutorials on <code>NSURLSession.uploadTaskWithStreamedRequest</code> and all of them i tried didn't work on swift 2.2</p>
  
  <p>I am using Xcode 7.3 Swift 2.2</p>
  
  <blockquote>
    <p>Points I Know:</p>
  </blockquote>
  
  <ol>
  <li><p>Background upload works only with File paths and not <code>NSData</code>. so i took the video url path from the <code>UIImagePickerController</code> function <code>func video(videoPath: NSString, didFinishSavingWithError error: NSError?, contextInfo info: AnyObject)</code></p></li>
  <li><p>To do background file transfer i have to enable them in the <code>Target</code>-><code>Capabilities</code> -> <code>Background Modes</code> -> <code>Background fetch</code></p></li>
  <li><p>Must set the Session manager with <code>NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("")</code>. But i don't know why and where that identifier is being used.</p></li>
  </ol>
  
  <p>I know this is a long post. But if it is answered this will surely be helpful to many developers.</p>