Swift alert: The effective way to show it with UIAlertController

Swift alert

Swift is known as the powerful programming language for iOS platforms. It was developed in 2014. This language is fully compatible with applications of Apple such as: iOS, macOS, watchOS and tvOS. Besides that, this swift comes with the open-source LLVM compiler framework. Therefore, Swift becomes the first programming language pick for lots of iOS app developers. Moreover, Swift has some great features for iOS app development. Besides that, one of the most important features of an iOS app is alerts. So we should consider UIAlertController. By using it, we can collect feedback and show the warning notification to app users. Furthermore, in iOS, swift alert has the below parts:

  • Firstly, title: It’s the name of the alert that the user sees.
  • Secondly, alert message: The descriptive text provides information about the notice.
  • Thirdly, alert Style: They are the kind of alert that will be displayed.
  • Fourthly, alert Actions: The actions that a user can perform to response to swift alert 
  • Finally, alert Textfield: It shows that the text field will accept any input of users

Thus, coming to this article, ArrowHiTech will review swift alert: The effective way to show it with UIAlertController. So let’s explore now.

Why should you choose Swift for your iOS app development? 

Swift is a newer programming language than other. But it has gained lots of successful achievement. Therefore, many famous firms in the world have trusted and used this language for their application development. They are LinkedIn, Pandora, Vimeo, Twitter, and Groupon. So now let’s explore the cool features of Swift:

  • Firstly, it is modern. Swift is the most recent programming language, developed by Apple systems. Furthermore, developers do not have to spend a lot of time coding Swift code. Because Swift codes are similar to ordinary English text. As a result, it’s much easier to find out the error code. 
  • Secondly is safety. You want to be sure that your mobile app is safe and secure. As a result, Swift is the most appropriate programming language for you. Because this programming language will reduce unsafe code classes. Besides that, Swift has protection against a variety of programming errors.
  • Thirdly, it is high speed performance. Swift is faster than Objective-C because of the combining of two files into a single code file. Swift-based apps will be approximately 3.9 times faster than Python-based apps. Therefore, this is the best thing that the developers prefer Swift than other competitors
  • Fourthly, less coding is required. Because it eliminates semicolons and brackets. Moreover,  in Swift, you also can use the “+” sign to combine two strings. Therefore, it is easier for you to write Swift code
  • Finally, Swift is open source. Therefore, you don’t need to pay any fee to use this. However, you still can use all great features of it. Therefore, you can save lots of money for the iOS app development cost. Besides that, Swift also meets all your development business requirements. However, you still can use all great features of it.

Introduction about Alert 

In iOS apps, alerts are useful. Because it will warn or inform users about important information regarding the action they are taking. For example, when an iOS mobile phone’s battery is low, it will notify users. So they can charge their phone before work is interrupted. In addition, an alert view will appear at the top of the iOS app content. Therefore, the user has to close it by hand before continuing using the app.

Swift alert

Alert has title, message and one or more buttons as above. Furthermore, we can not change alert style.

Alert Style

Firstly, the UIAlertControllerStyle Enum of Apple has alert styles. Moreover, there are two choices available. They are alert and actionsheet.

  • Firstly is .alert: This is a standard alert that appears in the application’s modality.
  • Secondly is .actionsheet: We need to display an action sheet in context to the ViewController. So we can use this option.

In addition, the below are example ways to add a new type of swift alert to your app.

Adding swift alert to the app

import UIKit

class ViewController: UIViewController {  

override func viewDidLoad() {  

super.viewDidLoad()  

 // Do any additional setup after loading the view.  

showAlert()  

}  

func showAlert() {  

let alert = UIAlertController(title: “Logout?”, message: “Are you sure you want to logout? You can login back to access your content.”, preferredStyle: .alert)  

alert.addAction(UIAlertAction(title: “Cancel”, style: UIAlertAction.Style.default, handler: { _ in  

 //Cancel Action  

}))  

alert.addAction(UIAlertAction(title: “Logout”,  

 style: UIAlertAction.Style.default,  

handler: {(_: UIAlertAction!) in  

//Sign out action  

}))  

DispatchQueue.main.async {  

self.present(alert, animated: false, completion: nil)  

}      

  }  

}  

Output     

alert Swift

Adding action sheet to the app

import UIKit  

class ViewController: UIViewController {  

override func viewDidLoad() {  

  super.viewDidLoad()  

// Do any additional setup after loading the view.  

showAlert()  

 }     

 func showAlert() {  

let alert = UIAlertController(title: “Logout?”, message: “Are you sure you want to logout? You can login back to access your content.”, preferredStyle: .actionSheet)  

alert.addAction(UIAlertAction(title: “Cancel”, style: UIAlertAction.Style.default, handler: { _ in  

 //Cancel Action  

}))  

alert.addAction(UIAlertAction(title: “Logout”,  

 style: UIAlertAction.Style.default,  

handler: {(_: UIAlertAction!) in  

 //Sign out action  

}))   

 DispatchQueue.main.async {  

self.present(alert, animated: false, completion: nil)  

}       

 }  

}  

Output

program language iOS
iOS program languageiOS program language

Adding Swift alert with Destructive action

func showAlertWithDestructiveBtn() {  

let alert = UIAlertController(title: “Logout?”, message: “Are you sure you want to logout? You can login back to access your content.”, preferredStyle: .alert)  

alert.addAction(UIAlertAction(title: “Cancel”, style: UIAlertAction.Style.destructive, handler: { _ in  

 //Cancel Action  

 }))  

alert.addAction(UIAlertAction(title: “Logout”,  

style: UIAlertAction.Style.default,  

handler: {(_: UIAlertAction!) in  

 //Sign out action  

 }))  

DispatchQueue.main.async {  

 self.present(alert, animated: false, completion: nil)  

 }        

 } 

Output

Swift

Adding Swift alert with more than two buttons

func showAlertWith2Btns() {  

let alert = UIAlertController(title: “Edit Quantity”, message: “Are you sure you want to edit the quantity”, preferredStyle: .alert)  

alert.addAction(UIAlertAction(title: “Dismiss”, style: UIAlertAction.Style.destructive, handler: { _ in  

 //Cancel Action  

 }))  

 alert.addAction(UIAlertAction(title: “Add 1 item”,  

 style: UIAlertAction.Style.default,  

handler: {(_: UIAlertAction!) in  

 //Add an item  

 }))  

 alert.addAction(UIAlertAction(title: “Remove 1 item”,  

 style: UIAlertAction.Style.default,  

 handler: {(_: UIAlertAction!) in  

 //Remove an item  

}))  

 DispatchQueue.main.async {  

self.present(alert, animated: false, completion: nil)  

 }      

 }  

Output

Swift notification

Adding Swift alert with TextField

func showAlertWithTF() {  

let alertController = UIAlertController(title: “Add new Items”, message: nil, preferredStyle: .alert)  

 let addAction = UIAlertAction(title: “Add”, style: .default) { (_) in  

if let txtField = alertController.textFields?.first, let text = txtField.text {  

// operations  

 }  

}  

let cancelAction = UIAlertAction(title: “Cancel”, style: .cancel) { (_) in }  

 alertController.addTextField { (textField) in  

 textField.placeholder = “Item name”  

 }  

alertController.addAction(addAction)  

 alertController.addAction(cancelAction)  

 DispatchQueue.main.async {  

 self.present(alertController, animated: true, completion: nil)  

 }  

 } 

Output

iOS program language

Conclusion

As mentioned before, Swift alert is important for iOS app development. Firstly, it will send warning notifications to iOS users. Thus, it helps users to be aware of their actions on the iOS app. Furthermore, in this blog, the ways to add a new type of swift alert to your app are shown clearly. This article includes both input and output of the ways to add a new type swift alert. Those are simple guides ArrowHiTech brings to you. Hence, you can read and apply it for your business easily. However, there are some other great features of Swift for you to explore. For example, Swift loop, Swift array,…

You want develop an iOS application. But you do not know how to do. If you want to know that, let’s ArrowHiTech help you. Because the ArrowHiTech website has many blogs about Swift. All of them are easy to read and follow. Hence, we believe that these blogs are helpful for your iOS app development. So you want to find out more information about the Swift programming language. Don’t hesitate. Feel free to visit us at the Swift and object- C.iOS App Development Services. We are waiting for helping you.

Tags

Share