0

I am trying to create and display an UIalert for my app. The alert is inside of the view controller and it will be called inside the api service call as follows.

    APIService().loginr(success: { result in
        print(result!)
        let json = result as! NSDictionary
        self.showResponseAlert(title: "Success!", message: json["message"] as? String)

    }, failure: {error in
        print(error!)
    }, parameters: parameters)

and my uiAlert

func showResponseAlert(title:String?,message:String?){
    let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "OK", style: .default))
    self.present(alert, animated: true, completion: nil)
}

and also i tried

   DispatchQueue.main.async{
       self.present(alertController, animated: true, completion: nil)
   }

but didn't work for me.

Dinithe Pieris
  • 1,822
  • 3
  • 32
  • 44

1 Answers1

3

Use this to present Alert.

func showResponseAlert(title:String?,message:String?){
    let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "OK", style: .default))
    if var topController = UIApplication.shared.keyWindow?.rootViewController {
            while let presentedViewController = topController.presentedViewController {
                topController = presentedViewController
            }
            topController.present(alert, animated: true, completion: nil)
     }
}
Dinithe Pieris
  • 1,822
  • 3
  • 32
  • 44