0

Is it possible to present the user with an error message, if you dont have any UIView/UIController?

I have a network framework where I would like to present an error, if some of the http requests is missing some data.

Like a simple UIAlertController or other:

let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
Chris G.
  • 23,930
  • 48
  • 177
  • 302
  • You can present it over UIWindow. Try it http://stackoverflow.com/questions/26554894/how-to-present-uialertcontroller-when-not-in-a-view-controller – ankit Apr 25 '17 at 11:46
  • You can get the current topmost view controller and present it from that. – Fogmeister Apr 25 '17 at 11:47
  • http://stackoverflow.com/questions/30570861/get-the-top-viewcontroller-in-ios-swift – Fogmeister Apr 25 '17 at 11:51
  • Another option is to go down the custom alert route and create a new window for your alert. I recently did this and packaged it into a framework but unfortunately my company won't let me share it. Making that window key and visible will show the alert over the top of any UI you have. – Jacob King Apr 25 '17 at 11:52

2 Answers2

0

Actually you can add a simple label/View on your window like below:

func showNavigationAlert(text:String,backgroundColor:UIColor = kNavigationBarColor,textColor:UIColor = .white,duration:Int = 2){
    let label = UILabel()
    label.font = appFont(fontSize: 11)
    label.textColor = textColor
    label.backgroundColor = backgroundColor
    label.text = text
    label.textAlignment = .center

    if let window = APPDELEGATE.window{
        window.addSubview(label)
        label.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 60)
        label.center.y -= 30
        UIView.animate(withDuration: 2, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: {
            label.center.y += 30
            }, completion: { (completed) in

                UIView.animate(withDuration: 0.3, animations: {
                    label.center.y -= 30
                    }, completion: { (c) in
                    label.removeFromSuperview()
            })
        })
    }

}

and in your network class just call

showNavigationAlert(text: parsedData["message"] as? String ?? "")

It will alert user in more sober way without annoying using alerts and will be disappeared automatically.

ankit
  • 3,537
  • 1
  • 16
  • 32
-2

You can use a UIAlertView to do just this, though be aware that this has been deprecated in favour of UIAlertController, and therefore it's availability isn't guaranteed in future Swift versions.

An example of it's usage can be seen below:

let alert = UIAlertView(title: "Error", message: "An error occurred.", delegate: nil, cancelButtonTitle: "OK")
alert.show()

Docs on UIAlertView can be found here.

Jacob King
  • 6,025
  • 4
  • 27
  • 45