Alamofire上传完成块在iOS 9和IOS 11上正常工作,但在iOS 10上却出错

问题描述 投票:1回答:3

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

  1. 隐藏进度警报,关闭当前视图控制器,显示祝贺警报
  2. 隐藏进度警报,显示错误警报

我的代码在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
            }
        })
ios alamofire swift4
3个回答
1
投票

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

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)
    }
}

0
投票

订单应该是。

  1. 驳回进展。
  2. 显示警报说一切都很好。 (这会掩盖界面,所以不可能触摸任何后面的东西)当在第2步轻拍确定。
  3. 关闭上传控制器。

只是为了更新代码:

无需切换到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

. . . . . .

© www.soinside.com 2019 - 2024. All rights reserved.