1

我正在尝试使用 AFNetworking > AFHTTPSessionManager Post 方法在后台上传视频。即使应用程序被暂停,我也希望它能够继续上传。但是我在执行这段代码时出错,它没有记录错误,并且在代码中没有指向任何地方。我尝试了关于我的场景的其他帖子中可用的所有解决方案,但无法使其正常工作。请检查下面的代码并建议我解决方案或将我链接到某个地方以使其工作

{
    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)
    }
}
4

0 回答 0