无法打开文件“xxx.mp4”,因为您无权查看它

时间:2017-03-31 02:12:52

标签: swift video permissions alamofire

我使用Alamofire方法multipartFormData.append(URL, withName: "file")将视频上传到服务器,但我收到了错误消息:

  

multipartEncodingFailed(Alamofire.AFError.MultipartEncodingFailureReason.bodyPartFileNotReachableWithError(file:///var/mobile/Media/DCIM/100APPLE/IMG_0939.mp4,Error Domain = NSCocoaErrorDomain Code = 257“文件”IMG_0939.mp4“由于您无权查看,因此无法打开。“UserInfo = {NSURL = file:///var/mobile/Media/DCIM/100APPLE/IMG_0939.mp4,NSFilePath = / var / mobile / Media / DCIM / 100APPLE / IMG_0939.mp4,NSUnderlyingError = 0x174450d10 {Error Domain = NSPOSIXErrorDomain Code = 1“不允许操作”}}))

我已打开隐私 - 照片库使用说明但我收到此错误,我不知道。

我的代码:

alamofireManager?.upload(
    multipartFormData: { multipartFormData in
        for i in 0..<videoURLs.count {
            multipartFormData.append(videoURLs[i], withName: "file")
        }
    },
    to: url + urlString,
    headers: headers,
    encodingCompletion: { encodingResult in
        switch encodingResult {
        case .success(let upload, _, _):
            upload.uploadProgress(closure: { (progress) in


                //Print progress
                //print(progress)
            })

            upload.responseJSON { response in
                if let value = response.result.value as? [String: AnyObject]{
                    success(value)
                }
            }
        case .failure(let encodingError):
            failture(encodingError)
        }
    }
)

1 个答案:

答案 0 :(得分:2)

您需要使用startAccessingSecurityScopedResource

if let url = file.url, url.startAccessingSecurityScopedResource() {
    multipartFormData.append(url, withName: "some-file.pdf")
    // After using the resource make sure you stop the access
    if url.startAccessingSecurityScopedResource() {
        url.stopAccessingSecurityScopedResource()
    }
}