Alamofire上传完成块可在iOS 9& IOS 11但在iOS 10上却出错了

时间:2018-02-22 00:58:03

标签: ios alamofire swift4

我希望Alamofire将数据上传到服务器并取决于结果:

  1. 隐藏进度警报,关闭当前视图控制器,显示 祝贺警报
  2. 隐藏进度提示,显示错误提醒
  3. 我的代码在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
                }
            })
    

3 个答案:

答案 0 :(得分:1)

使用completition处理程序并使用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)
    }
}

答案 1 :(得分:0)

在控制器上显示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)
    }
}

答案 2 :(得分:0)

订单应该是。

  1. 驳回进度。
  2. 显示警告说一切都很好。 (这会掩盖界面,所以不可能触摸 任何背后的事情)当在第2步轻拍确定。
  3. 关闭上传控制器。
  4. 只是为了更新代码:

    无需在生成的闭包输出上切换到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
    

    。 。 。 。 。