How can I add an alert view [duplicate] - ios

This question already has answers here:
How would I create a UIAlertView in Swift?
(36 answers)
Closed 7 years ago.
I would like to be able to add an alert view to my login screen that says Incorrect Username/ Password when the info submitted is incorrect. I am using Swift 2. I'm still new to learning code and I am unsure how to add this action.

Just use this code to display the alert:
func wrongLogin(input: String) {
var alert = UIAlertController(title: "Incorrect", message: "The \(input) is wrong. Try again.", preferredStyle: UIAlertControllerStyle.Alert)
//add a cancel button
alert.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.Cancel, handler: nil))
// Show it
showViewController(alert, sender: self)
}
Then when calling it just pass in if it's the username or the password.
You can do that like this wrongLogin("password") or wrongLogin("username")
For further info see this answer.
Hope that helps, Julian.

Related

In Swift how to create Custom Alert View [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have to implement a Custom alerts in my App programmatically using Swift language. i tried implementing using some third party library "SCLAlertView" but not able to understand from that, i need a implement simple alert pop ups with dynamic message and number of button changes in App. As there were lots of AlertView in my App. so i need to update the dynamically.
below i have attached an sample image of custom alert how it looks to implement
Please help me to implement this feature.
After the installing pod with pod SCLAlertView
You can choose Alert view Style and Alert View Animation style with these enums
enum SCLAlertViewStyle: Int {
case Success, Error, Notice, Warning, Info, Edit, Wait
}
public enum SCLAnimationStyle {
case NoAnimation, TopToBottom, BottomToTop, LeftToRight, RightToLeft
}
SCLAlertView has many Control groups like add textField , buttons and icons
here is a adding button function codes
let alertView = SCLAlertView()
alertView.addButton("First Button", target:self, selector:Selector("firstButton"))
alertView.addButton("Second Button") {
println("Second button tapped")
}
alertView.showSuccess("Button View", subTitle: "This alert view has buttons")
and Alert view custom types
SCLAlertView().showError("Hello Error", subTitle: "This is a more descriptive error text.") // Error
SCLAlertView().showNotice("Hello Notice", subTitle: "This is a more descriptive notice text.") // Notice
SCLAlertView().showWarning("Hello Warning", subTitle: "This is a more descriptive warning text.") // Warning
SCLAlertView().showInfo("Hello Info", subTitle: "This is a more descriptive info text.") // Info
SCLAlertView().showEdit("Hello Edit", subTitle: "This is a more descriptive info text.") // Edit
in Github Page you will find many beatiful desing alert views , it is easy to use
https://github.com/vikmeup/SCLAlertView-Swift
Just add different UIAlertAction to your UIAlertController whenever you want it to.
let alertAction: UIAlertAction = UIAlertAction(title: "YES", style: UIAlertActionStyle.Default, handler: {
//Code goes here
})
let alertAction2: UIAlertAction = UIAlertAction(title: "NO", style: UIAlertActionStyle.Default, handler: {
//Code goes here
})
let alertAction3: UIAlertAction = UIAlertAction(title: "Maybe", style: UIAlertActionStyle.Default, handler: {
//Code goes here
})
alert.addAction(alertAction)
alert.addAction(alertAction2)
alert.addAction(alertAction3)
You can dynamically add UIAlertAction's to your UIAlertController depending on your needs. If you only need two buttons, then don't add alertAction3. If you need three or four, then add them as necessary.

Swift iOS: fatal error when dismissing alert controller while keyboard is up

So, I’ve got a view controller with a number of text fields on it. The first two text fields need text entered into them before submitting. I check the first two text fields and display an alert controller in case they’re empty.
let alertController = UIAlertController(title: "Error", message:
"Please enter some text.", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.Default,handler: nil))
self.presentViewController(alertController, animated: true, completion: nil)
This works fine, as long as the keyboard is dismissed before trying to submit the form. If the keyboard is still showing however, the app crashes with fatal 'error: unexpectedly found nil while unwrapping an Optional value'
What is happening here?
Just solved this myself by adding view.endEditing(true) before displaying the alert controller. Would still like to know why it was crashing in the first place.

Exit application using a UIAlertController

If a user opens up the application without an internet connection, a window pops up that says a connection is required, and there is an ok button. I want to the ok button to exit the application. Here is what I have:
if !isConnectedToNetwork(){
let alert = UIAlertController(title: "No Internet", message: "You need an internet connection to use this app", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
I am going to use this to exit the app:
UIControl().sendAction(Selector("suspend"), to: UIApplication.sharedApplication(), forEvent: nil)
I just don't know how to connect it to the OK button above.
Don't. Apple will reject this (if they see it).
Simply inform the user and add a 'retry' button. The retry button should obviously check the connection again.
To actually answer the question, you have currently set the handler: nil on the button action, instead you can actually set a handler and use it to call whatever logic you like.
You can handle when user press OK by the following code
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default,
handler: { (action:UIAlertAction!) -> Void in
//after user press ok, the following code will be execute
NSLog("User pressed OK!!")
}))

How to display a dialog with (multiselect listview) before displaying "app would like to send you push notifications" dialog box

I would like to display a pop-up where I'll ask the user about their preferences related to the push notifications and for that one, I want to display a list of options to the user. User can select more than one options.
I think that I'll have to display a tableview inside the UIAlertView, but it is deprecated now. So, how can I display a pop (with some small message + multiple select list ) before the APN system permissions dialog in Swift.
Any help will be appreciated.
You can use this code:
let alert = UIAlertController(title: title, message:message, preferredStyle: .Alert)
let action = UIAlertAction(title: "OK", style: .Default) { _ in
acceptNotification = true //code to execute when the user taps that OK
}
alert.addAction(action)
//you can add more actions
self.presentViewController(alert, animated: true){ // this part if provided, will be invoked after the dismissed controller's viewDidDisappear: callback is invoked.
}

UIAlertView And UIAlertController

I am using SWIFT in xcode 6 to develope my application AND needs to support the APP for iphone 4 and later versions... So have selected the 'Deploment Target' as 7.1. In simple words needs to support iOS7, iOS8 AND iOS9...
When using Alert View I came across in many places discussing now we have to use newly introduced 'UIAlertController' rather than the old 'UIAlertView'...
By reading got to know UIAlertView is deprecated from ios 8.
But in my case as I have to support for ios 7.1 AND I can NOT only use 'UIAlertController'.
I started using as the following way as many tutorials explains...
if (objc_getClass("UIAlertController") != nil)
{
// Use UIAlertController
}
else
{
// Use UIAlertView
}
But in this way got to write the same code twice and really annoyiong... Either I have to create a custom Alertview combining both or needs to continue coding like this....
but just to test I've used only UIAlertView (ignoring UIAlertController) and the app runs fine even in ios 8 simulators... But the document says UIAlertView is deprecated from iOS 8.0...
So my question is, Like to hear what the best practice to continue my app with these API changes... Is it alright if I ignore 'UIAlertController' and work with old 'UIAlertView' until stop support for iOS7 one day... Will that effect in to my app in any way bad? Thanks
I've written a wrapper around these two classes which uses appropriate classes according to iOS version.
find it here
https://github.com/amosavian/ExtDownloader/blob/master/Utility%2BUI.swift
to show a simple alert use this:
Utility.UI.alertShow("message", withTitle: "title", viewController: self);
to show an action view: use this code:
let buttons = [Utility.UI.ActionButton(title: "Say Hello", buttonType: .Default, buttonHandler: { () -> Void in
print("Hello")
})]
Utility.UI.askAction("message", withTitle: "title", viewController: self, anchor: Utility.UI.AnchorView.BarButtonItem(button: uibarbuttontapped), buttons: buttons)
In case you are presenting action in iPhone, it will add a cancel button automatically. But you can have custom cancel button by defining a button with type of ".Cancel"
If you want show a modal alert view, use this code:
let cancelBtn = Utility.UI.AlertButton(title: "Cancel", buttonType: .Cancel, buttonHandler: nil)
let okBtn = Utility.UI.AlertButton(title: "OK", buttonType: .Default, buttonHandler: { (textInputs) -> Void in
print("OK button pressed")
})
Utility.UI.askAlert("message", withTitle: "title", viewController: self, buttons: [cancelBtn, okBtn], textFields: nil)
As you can see, the text fields is set to nil above. You can set it if you want ask something from user, like this:
let cancelBtn = Utility.UI.AlertButton(title: "Cancel", buttonType: .Cancel, buttonHandler: nil)
let okBtn = Utility.UI.AlertButton(title: "OK", buttonType: .Default, buttonHandler: { (textInputs) -> Void in
print("You entered" + textInputs[0])
}
})
let textFields = [Utility.UI.AlertTextField(placeHolder: "enter secret text here", defaultValue: "", textInputTraits: TextInputTraits.secretInput())]
Utility.UI.askAlert("Enter password", withTitle: "title", viewController: self, buttons: [cancelBtn, okBtn], textFields: textFields)
You can customize almost everything. e.g by defining custom textInputTrait you can customize text inputs easily. also you have not to deal with delegates, instead simple closures are there. Please read code to find out more.

Resources