1

I am trying to upload a video in background using AFNetworking > AFHTTPSessionManager Post method. I want it to conitune uploading even if the app is suspended. But I get error while executing this code, it logs no error, and points to nowhere in code. I tried every solution available on other posts regarding my scenario, but couldn't get it working. Plesae check my code below and suggest me solution or link me somewhere to make it work

{
    let urlString = BaseUrl + WebLinks.post

    let appId = NSBundle.mainBundle().bundleIdentifier

    let config = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier(appId!)// crashes with it
    //let config = NSURLSessionConfiguration.defaultSessionConfiguration()//doesnt work in background

    config.allowsCellularAccess = true
    config.timeoutIntervalForRequest = NSTimeInterval(999)
    config.timeoutIntervalForResource = NSTimeInterval(999)

    let manager = AFHTTPSessionManager(baseURL: NSURL(string: urlString), sessionConfiguration: config)
    manager.responseSerializer.acceptableContentTypes = NSSet(object: "application/json") as Set<NSObject>
    manager.requestSerializer.setValue(PersistenceManager.sharedInstance.accessToken, forHTTPHeaderField: "Authorization")

    let param = [
        "subject":subject,
    ]
    print(urlString)

    manager.POST(urlString, parameters: param, constructingBodyWithBlock: { (formData) -> Void in

        if var imageToUpload = image {
            imageToUpload = imageToUpload.resizeImage(1200)
            let imageData = UIImageJPEGRepresentation(imageToUpload, 0.9);
            formData.appendPartWithFileData(imageData, name: "post_image", fileName: "picture.jpg", mimeType: "image/jpeg")
        }
        if let videoUrl = video {

            //let videoData = NSData(contentsOfURL: videoUrl)
            //formData.appendPartWithFileData(videoData, name: "post_video", fileName: "video.mp4", mimeType: "video/quicktime")
            do{
                try formData.appendPartWithFileURL(videoUrl, name: "post_video", fileName: "video.mp4", mimeType: "video/quicktime")
            }catch{
                print("Failed to attach video")
            }
        }

        }, success: { (nsURLSessionDataTask, response) -> Void in
            print (response)
        }) { (nsURLSessionDataTask, error) -> Void in
            print (error.localizedDescription)
    }
}
Raheel Sadiq
  • 9,847
  • 6
  • 42
  • 54
  • This question is a bit old, but it looks like the problem _might_ have been that the video is an mp4, but the mimeType is `video/quicktime`, you might try it with `mimeType: 'video/mp4'` – BananaNeil Feb 09 '16 at 02:42
  • 1
    @BananaNeil The problem wasn't mimetype, the problem was: I was trying to use the uploadTask which is not right in my scenario. What I needed was to use NSUrlSession with BackgroundConfigurations, and use its delegate methods to keep track of the status. Plus iPhone makes .mov video, 'video/quicktime' just worked fine. Plus now I am thinking of answering my own question :) – Raheel Sadiq Feb 09 '16 at 06:39
  • Hah, you totally should. – BananaNeil Feb 09 '16 at 19:52

0 Answers0