我希望Alamofire将数据上传到服务器并取决于结果:
我的代码在iOS 9和11上工作正常,但在成功案例的iOS 10上只隐藏进度警报。用户感到困惑并一次又一次地提交表单
Alamofire.upload(multipartFormData: { MultipartFormData in
MultipartFormData.append((self.model.name?.data(using: String.Encoding.utf8)!)!, withName: "Form[name]")
MultipartFormData.append((self.model.category?.description.data(using: String.Encoding.utf8)!)!, withName: "Form[category]")
if (self.filePreview.count>0) {
for (index,preview) in self.filePreview.enumerated() {
if preview != nil
let data = UIImageJPEGRepresentation((preview?.getImage())!, 80)
MultipartFormData.append(data!, withName: "Form[files]["+String(describing:index)+"]", fileName: "attachment"+String(describing: index)+".JPG", mimeType: preview!.getMime())
}
}
}
debugPrint(MultipartFormData)
}, to: url, encodingCompletion: { (result) in
switch result {
case .success( _, _, _):
progressAlert.dismiss(animated: true, completion: nil)
self.dismiss(animated: true, completion: nil)
let doneAlert = UIAlertController(title: "Success", message: "Your message was sent", preferredStyle: .alert)
let donedOk = UIAlertAction(title: "OK", style: .default, handler: nil)
doneAlert.addAction(donedOk)
self.present(doneAlert, animated: true, completion: nil)
break
case .failure( _):
progressAlert.dismiss(animated: true, completion: nil)
let doneAlert = UIAlertController(title: "Failed", message: "Your message was not sent", preferredStyle: .alert)
let donedOk = UIAlertAction(title: "OK", style: .default, handler: nil)
doneAlert.addAction(donedOk)
self.present(doneAlert, animated: true, completion: nil)
break
}
})
使用完成处理程序并使用UIApplication.shared.keyWindow?.rootViewController
查找当前顶级ViewController解决的问题。将显示进度警报隐藏,当前视图控制器关闭和成功警报。码:
progressAlert.dismiss(animated: true) {
let doneAlert = UIAlertController(title: "Отправлено", message: "Your message was sent", preferredStyle: .alert)
let donedOk = UIAlertAction(title: "Success", style: .default, handler: nil)
doneAlert.addAction(donedOk)
self.dismiss(animated: true) {
let presentingVC = UIApplication.shared.keyWindow?.rootViewController
presentingVC?.present(doneAlert, animated: true, completion: nil)
}
}
在控制器上显示UIAlertController
的问题,该控制器的视图不在窗口层次结构中,首先你要解除控制器并在被解雇的控制器上显示UIAlertController
。
您必须参考当前控制器所呈现的控制器,然后解除当前控制器存在的前一个控制器上的UIAlertController
。
更新主调度队列上的用户界面。
DispatchQueue.main.async {
let controller = self.presentingViewController
self.dismiss(animated: true) {
let doneAlert = UIAlertController(title: "Success", message: "Your message was sent", preferredStyle: .alert)
let donedOk = UIAlertAction(title: "OK", style: .default, handler: nil)
doneAlert.addAction(donedOk)
controller?.present(doneAlert, animated: true, completion: nil)
}
}
订单应该是。
只是为了更新代码:
无需切换到MainQueue,Math Thompson对生成的闭包输出到DispatchQueue.main.async。
所以从我的观点来看,互动应该是这样的:
switch result {
case .success( _, _, _):
//1
progressAlert.dismiss(animated: true, completion: nil)
//2
let doneAlertController = UIAlertController(title: "Success", message: "Your message was sent", preferredStyle: .alert)
let donedOk = UIAlertAction(title: "OK", style: .default) { (action) in
//3
self.dismiss(animated: true, completion: nil)
}
doneAlertController.addAction(donedOk)
self.present(doneAlertController, animated: true, completion: nil)
break
. . . . . .