AFHTTPSessionManager публикует видео swift

Я пытаюсь загрузить видео в фоновом режиме, используя 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 ответов

Другие вопросы по тегам