我正在使用alamofire作为我的注册表单。我有一个个人资料图片和其他一些字段,如电子邮件,用户名,密码,password_confirmation。 数据必须以原始json形式发布。
let signupBag = [
"member_profile": [
"photo": "URL",
"user_attributes": [
"email": "\(txtEmail.text)",
"username": "\(txtName.text)",
"password": "\(txtPassword.text)",
"password_confirmation": "\(txtConfirmPwd.text)"
]
]
]
NVActivityIndicatorPresenter.sharedInstance.startAnimating(activityData)
HelperClass(). doPostMultiPartFormRequest(url: BASE_URL+SIGNUP , param: signupBag, completion: { (dataResponse,swiftJSON,error) in
guard error == nil else
{
NVActivityIndicatorPresenter.sharedInstance.stopAnimating()
HelperClass.showAlertViewWithTitle(title: "Error", Text: "\(error!.localizedDescription)", controllerToShowOn: self)
return
}
print(swiftJSON!)
if swiftJSON!["errors"].stringValue != ""
{
NVActivityIndicatorPresenter.sharedInstance.stopAnimating()
HelperClass.showAlertViewWithTitle(title: swiftJSON!["message"].stringValue, Text: swiftJSON!["errors"].stringValue, controllerToShowOn: self)
}else{
NVActivityIndicatorPresenter.sharedInstance.stopAnimating()
self.performSegue(withIdentifier: "showProfile", sender: self)
}
NVActivityIndicatorPresenter.sharedInstance.stopAnimating()
})
Helperclass方法
func doPostMultiPartFormRequest(url:String, param:[String:Any], completion:@escaping (JSON?,Error?) -> Void)
{
print("Method indicator")
Alamofire.upload(multipartFormData: { (formData) in
//setting normal params
for (key, value) in param {
if value is String
{
formData.append((value as! String).data(using: String.Encoding.utf8)!, withName: key)
}
}
//setting data params
for (key, value) in param {
if value is UIImage
{
let imageData = UIImageJPEGRepresentation(value as! UIImage,0.3)!
formData.append(imageData, withName: key, fileName: "myImage.png", mimeType: "image/png")
}
}
}, to: url, encodingCompletion:
{
(encodingResult) -> Void in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON{ response in
guard response.result.error == nil else {
print(upload.progress)
completion(nil, response.result.error)
return
}
if let value = response.result.value
{
completion(JSON(value),nil)
}
}
case .failure(let error):
completion(nil, error)
break
}
}
)
}
但我得到“无法连接到服务器”。我正在使用alamofire和swiftyJson。