Ganesh Ganesh - 1 year ago 128
Objective-C Question

AlertView with TextField in DidFinishLaunchingWithOptions

I have a scenario like, In First installation of app, user should view an

, User should enter email id, if it is valid dismiss the
, else without dismissing that
need to prompt other
Saying "Enter Valid Email ID", how to Achieve this?

Answer Source

Add Some logic For Your didFinishLaunchingWithOptions.

Here Scenario is that it will first prsent Alert saying Enter your email. When you enter Email and click OK it check whether Email is valid or not. if it is not Valid present another Alert showing your email is not valid. meanwhile your first Alert will be dismissed at that time. when you again Press OK of AlertError. Again it will show AlertController of your textField Email.


    UIAlertController * alertController = [UIAlertController alertControllerWithTitle: @"Your Email" message: @"Enter Your Email"preferredStyle:UIAlertControllerStyleAlert];

    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"E-mail";
        textField.textColor = [UIColor blueColor];
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField.borderStyle = UITextBorderStyleRoundedRect;

    [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSArray * textfields = alertController.textFields;
        UITextField * Email = textfields[0];

        if ([self validateEmailWithString:Email.text]) {
            UIAlertController *alertError = [UIAlertController  alertControllerWithTitle:@"Error"  message:nil  preferredStyle:UIAlertControllerStyleAlert];

            [alertError addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
            [self dismissViewControllerAnimated:YES completion:nil];

            [self presentViewController:alertController animated:YES completion:nil];


            [self presentViewController:alertError animated:YES completion:nil];
    [self presentViewController:alertController animated:YES completion:nil];

- (BOOL)validateEmailWithString:(NSString*)checkString
    BOOL stricterFilter = NO;
    NSString *stricterFilterString = @"[A-Z0-9a-z\\._%+-]+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2,4}";
    NSString *laxString = @".+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2}[A-Za-z]*";
    NSString *emailRegex = stricterFilter ? stricterFilterString : laxString;
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:checkString];

This will work Perfect for You.

enter image description here

enter image description here

Now if there is error again present email alert.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download