Home > database >  File is not opening after Downloading has been completed iOS swift
File is not opening after Downloading has been completed iOS swift

Time:04-28

When downloading completed I'm showing the alert that download has been completed after that when user click on dismiss button in alert popup self.quickLook(url: url) func will call. But not showing the file in webView. When removing the alert code everything working fine and file opening in webView.

 func showAlerts(){
        let alertController = UIAlertController(title: "Download", message: "Download Completed", preferredStyle: .alert)
        alertController.addAction(UIAlertAction(title: "dismiss", style: .default, handler: { _ in
            self.dismiss(animated: true, completion: nil)
        }))
        alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
        self.present(alertController, animated: true, completion: nil)
        
    }
   
    @IBAction func openDoc(_ sender: UIButton) {
        if let url = URL(string: "https://ikddata.ilmkidunya.com/images/books/12th-class-chemistry-chapter-10.pdf") {
            self.loadFileAsync(url: url) { response, error in
                if error == nil {
                    self.showAlerts()
                    self.quickLook(url: url)
                }
            }
        }
    }

Check console screenshot Console screenshot see msg

CodePudding user response:

How's your quickLook implemented? I guess it also calls present. You cannot present two things at the same time on iOS.

An option would be to quickLook after the alert is dismissed. Something like:

func showAlerts(and onDismiss: (() -> Void)? = nil) {
  let alertController = UIAlertController(title: "Download", message: "Download Completed", preferredStyle: .alert)
  alertController.addAction(UIAlertAction(title: "dismiss", style: .default, handler: { _ in
    self.dismiss(animated: true) { onDismiss?() }
  }))
  alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

  present(alertController, animated: true, completion: nil)
}
   
@IBAction func openDoc(_ sender: UIButton) {
  if let url = URL(string: "https://ikddata.ilmkidunya.com/images/books/12th-class-chemistry-chapter-10.pdf") {
    loadFileAsync(url: url) { response, error in
      if error == nil {
        self.showAlerts { self.quickLook(url: url) }
      }
    }
  }
}

Judging from your words it's exactly what you expect.

  • Related