AFHTTPSessionManager发布视频swift

时间:2015-09-21 07:08:08

标签: ios afnetworking-2 nsurlsession nsurlsessionconfiguration afhttpsessionmanager

我正在尝试使用AFNetworking>在后台上传视频AFHTTPSessionManager Post方法。即使应用程序被暂停,我也希望它能够上传。但是我在执行此代码时遇到错误,它没有记录错误,并且在代码中指向无处。我尝试了其他帖子中有关我的方案的所有解决方案,但无法使其正常运行。 Plesae检查下面的代码,并建议我解决方案或链接我在某处使其工作

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

0 个答案:

没有答案