Sunday 7 January 2018

Creating Alerts

Greetings i am sharing some of useful code for IOS in Swift language that i have usually used in my apps. 😊 

While Developing the apps we often need to show the alerts at some situations.
Like we may give greetings to Users, show alert when any thread has perform any particular task, to give some error message regarding Network, background response etc.

From here we can alert the user:

let title = "Application Name"
        let message = "Hey! lets do some code in the Swift Language."
        let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "Yes Continue", style: UIAlertActionStyle.default, handler: { (alert: UIAlertAction) in
            /*
             Perform Yes Action Here
             */
        }))
        alert.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.default, handler: nil))
        
        DispatchQueue.main.async(){
            inObject.present(alert, animated: true, completion: nil)

        }


By running that an alert pop up will come up.
The use of DispatchQueue.main.async(){} is optional. Its just used to show the alert immediately when some background task is being performed.

Instead of the UIAlert actionsheet can be used to alert the user. To use that just use the---> preferredStyle: UIAlertControllerStyle.sheet

UIActionsheet will get open with the appropriate actions.



These are the default alerts we commonly used.
Some times we may need to show some interactive alerts.
Lets create the Custom Alerts.
will update in few hours.