I'm using Alamofire 4.0 to upload video to server after select or record it through device/camera but when I'm trying to call upload function with append , this error appeared to me in all append statement , what is the wrong in my code.
the second my question about if I want to display progress with percentage of progress during upload how I can make that through Alamofire.
Thanks :)
My code after reading url of selected/recorded video
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let mediaType:AnyObject? = info[UIImagePickerControllerMediaType] as AnyObject?
if let type:AnyObject = mediaType {
if type is String {
let stringType = type as! String
if stringType == kUTTypeMovie as String {
let urlOfVideo = info[UIImagePickerControllerMediaURL] as? NSURL
if let url = urlOfVideo {
// 2
print(url)
let URL = try! URLRequest(url: "myurl", method: .post, headers: ["Authorization": "auth_token"])
Alamofire.upload(multipartFormData: { multipartFormData in
multipartFormData.append(url, withName: "videoFile", fileName: "filename", mimeType: "mov")
multipartFormData.append("video", withName: "load")
multipartFormData.append("record", withName: "type")
}, with: URL, encodingCompletion: { (result) in
// code
})
}
}
}
}
picker.dismiss(animated: true, completion: nil)
}
We can do this.
With Extension
Regarding the error message:
If we take a look at the
append
methods ofMultipartFormData
of Alamofire:We note that no
append(...)
method allows a first argument of typeString
, which is what you, however, attempt to use when appending in yourmultipartFormData
closure ofAlamofire.upload
.I believe you're attempting to use the following method:
In which case you need to encode your string into the Swift type
Data
, e.g. as follows:As for your call:
The immutable
url
above is of of typeNSURL
. In Swift 3, you should prefer using Foundation typeURL
instead, which bridge toNSURL
, but is not the same type. We see in Alamofire 4 that it particularly expects theURL
type for theappend
function you attempt to call above:You've noted yourself that you may use a workaround to call this method by using the
absoluteURL
property ofNSURL
upon your instanceurl
; but this simply yields an optional of typeURL
. A better approach would simply be using theURL
type rather thanNSURL
from the start.