-
Notifications
You must be signed in to change notification settings - Fork 0
9. Chained Alerts
Gabriel Theodoropoulos edited this page Apr 17, 2019
·
1 revision
The following snippet demonstrates how we can chain the presentation of different alert controllers. In this example, we simulate the login process and we use three alerts for that.
- The first one, is an alert with two textfields where users enter email and password.
- The second alert is an activity alert displaying a wait message.
- The third and last alert is a single button alert that informs about the successful connection to the fake account.
Notice in the next code that when there's user interaction with the alert, there's no need for us to dismiss the alert. But in case where there's no interaction (the alert with the activity indicator), it's our job to manually dismiss the alert.
GTAlertCollection.shared.presentMultipleTextFieldsAlert(withTitle: "Login", message: "Connect to your account:", doneButtonTitle: "Login", cancelButtonTitle: "Cancel", numberOfTextFields: 2, configurationHandler: { (textFields, didFinishConfiguration) in
// Make sure that the required number of textfields exist.
if let textFields = textFields {
if textFields.count == 2 {
// Configure both textfields.
textFields[0].placeholder = "Email"
textFields[0].textContentType = UITextContentType.emailAddress
textFields[1].placeholder = "Password"
textFields[1].isSecureTextEntry = true
// Call the following to let the alert get presented.
didFinishConfiguration()
}
}
}) { (textFields) in
// The Login action button has been tapped in this case.
// Check if the proper number of textfields exist.
if let textFields = textFields {
if textFields.count == 2 {
// For the sake of the example, just check if both textfields have values,
// we don't care about the actual values right now.
if textFields[0].text != "" && textFields[1].text != "" {
// There is no need to dismiss the alert manually,
// as it's being dismissed when the Login button is tapped.
// Show a new alert with an activity indicator and a message saying to wait.
GTAlertCollection.shared.presentActivityAlert(withTitle: "Please Wait", message: "You are being connected to your account...", activityIndicatorColor: .red, showLargeIndicator: true, presentationCompletion: { (success) in
if success {
// The new alert has been presented.
// In a real application, this would be the place where the actual connection
// would start taking place.
// In this example we'll dismiss the activity alert after 2 seconds.
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0, execute: {
// Time to dismiss the alert.
GTAlertCollection.shared.dismissAlert(completion: {
// After it has been dismissed, we'll present a new one to welcome
// the connected user and inform that the connection was successful.
GTAlertCollection.shared.presentSingleButtonAlert(withTitle: "Welcome", message: "You are now connected to your account!", buttonTitle: "OK", actionHandler: {
})
})
})
}
})
}
}
}
}
Here's a demo of the code above: