如何在后台上传视频文件?
How to Upload video files in background?
我正在开发一个应用程序,我需要在其中上传从 iPhone 相机拍摄的视频并将其上传到服务器。当应用程序处于前台时,视频会被上传,但我不知道当应用程序处于非活动状态时如何在后台进行。我使用 AFNetworking
进行分段数据上传。这是我试过的代码
var task:NSURLSessionUploadTask!
let FILEPICKER_BASE_URL = "My server Url"
var isUploading : Bool = false
var videoPath : String!
func upload(dictMain: NSMutableDictionary)
{
isUploading = true
let uuid = NSUUID().UUIDString + "-" + (getUserId().description)
let request = AFHTTPRequestSerializer().multipartFormRequestWithMethod("POST", URLString: FILEPICKER_BASE_URL, parameters: nil, constructingBodyWithBlock: { (fromData) in
do {
try fromData.appendPartWithFileURL(NSURL(fileURLWithPath: videoPath) , name: "fileUpload", fileName: uuid, mimeType: "video/quicktime")
}catch{
print(error)
}
}, error: nil)
let manager:AFURLSessionManager = AFURLSessionManager(sessionConfiguration: NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("Your video is uploading"));
task = manager.uploadTaskWithStreamedRequest(request, progress: nil , completionHandler: { (response, responseObject, error) in
NSLog("Resposne Object: \(responseObject)")
post(dictMain,response: responseObject!)
})
task.resume()
}
我也不知道怎么知道我的上传进度。我尝试在进度参数
中使用以下块
{ (progress) in
print("\((progress))")
}
但是它不起作用编译器显示错误
Cannot convert value of type '(_) -> _' to expected argument type 'AutoreleasingUnsafeMutablePointer<NSProgress?>' (aka 'AutoreleasingUnsafeMutablePointer<Optional<NSProgress>>')
谁能分享一个真正有效的片段。当我用谷歌搜索时,关于 NSURLSession.uploadTaskWithStreamedRequest
的基本教程非常少,而且我尝试过的所有教程都不适用于 swift 2.2
我用的是Xcode7.3Swift2.2
Points I Know:
后台上传仅适用于文件路径,不适用于 NSData
。所以我从 UIImagePickerController
函数 func video(videoPath: NSString, didFinishSavingWithError error: NSError?, contextInfo info: AnyObject)
中获取了视频 url 路径
要进行后台文件传输,我必须在 Target
->Capabilities
-> Background Modes
-> Background fetch
[= 中启用它们24=]
必须使用 NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("")
设置会话管理器。但我不知道为什么以及在哪里使用该标识符。
我知道这很长post。但如果得到解答,这肯定会对许多开发人员有所帮助。
我通过调用 DispatchQueue Background queue
中的上传功能修复了它
DispatchQueue.global(qos: .background).async { upload() }
我正在开发一个应用程序,我需要在其中上传从 iPhone 相机拍摄的视频并将其上传到服务器。当应用程序处于前台时,视频会被上传,但我不知道当应用程序处于非活动状态时如何在后台进行。我使用 AFNetworking
进行分段数据上传。这是我试过的代码
var task:NSURLSessionUploadTask!
let FILEPICKER_BASE_URL = "My server Url"
var isUploading : Bool = false
var videoPath : String!
func upload(dictMain: NSMutableDictionary)
{
isUploading = true
let uuid = NSUUID().UUIDString + "-" + (getUserId().description)
let request = AFHTTPRequestSerializer().multipartFormRequestWithMethod("POST", URLString: FILEPICKER_BASE_URL, parameters: nil, constructingBodyWithBlock: { (fromData) in
do {
try fromData.appendPartWithFileURL(NSURL(fileURLWithPath: videoPath) , name: "fileUpload", fileName: uuid, mimeType: "video/quicktime")
}catch{
print(error)
}
}, error: nil)
let manager:AFURLSessionManager = AFURLSessionManager(sessionConfiguration: NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("Your video is uploading"));
task = manager.uploadTaskWithStreamedRequest(request, progress: nil , completionHandler: { (response, responseObject, error) in
NSLog("Resposne Object: \(responseObject)")
post(dictMain,response: responseObject!)
})
task.resume()
}
我也不知道怎么知道我的上传进度。我尝试在进度参数
中使用以下块 { (progress) in
print("\((progress))")
}
但是它不起作用编译器显示错误
Cannot convert value of type '(_) -> _' to expected argument type 'AutoreleasingUnsafeMutablePointer<NSProgress?>' (aka 'AutoreleasingUnsafeMutablePointer<Optional<NSProgress>>')
谁能分享一个真正有效的片段。当我用谷歌搜索时,关于 NSURLSession.uploadTaskWithStreamedRequest
的基本教程非常少,而且我尝试过的所有教程都不适用于 swift 2.2
我用的是Xcode7.3Swift2.2
Points I Know:
后台上传仅适用于文件路径,不适用于
NSData
。所以我从UIImagePickerController
函数func video(videoPath: NSString, didFinishSavingWithError error: NSError?, contextInfo info: AnyObject)
中获取了视频 url 路径
要进行后台文件传输,我必须在
Target
->Capabilities
->Background Modes
->Background fetch
[= 中启用它们24=]必须使用
NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("")
设置会话管理器。但我不知道为什么以及在哪里使用该标识符。
我知道这很长post。但如果得到解答,这肯定会对许多开发人员有所帮助。
我通过调用 DispatchQueue Background queue
DispatchQueue.global(qos: .background).async { upload() }