问题
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)
}
}
来源:https://stackoverflow.com/questions/32689244/afhttpsessionmanager-posting-video-swift