使用适用于iOS的firebase验证电子邮件

时间:2013-04-03 22:01:38

标签: ios email-validation firebase

提交创建新用户的请求时,如果用户尝试使用无效的电子邮件提交,则应用程序崩溃(I.E.没有@符号)。有没有办法通过Firebase检查电子邮件是否有效,还是我需要验证应用程序端的内容?

我在控制台中遇到的错误是:

* 因未捕获的异常'InvalidEmail'而终止应用,原因:'jfkdsla; fjdk是无效的电子邮件地址'

NSString *url = [NSString stringWithFormat:@"https://myapp.firebaseio.com/users/%@", displayName.text];
    Firebase *dataRef = [[Firebase alloc] initWithUrl:url];
    [dataRef observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {

        NSLog(@"Ref %@", snapshot.value);
        snapshotValue = snapshot.value;
    }];

    if (snapshotValue == NULL) {
        AppDelegate *appDelegate= (AppDelegate *)[[UIApplication sharedApplication] delegate];

        [appDelegate.authClient createUserWithEmail:email.text password:password.text andCompletionBlock:^(NSError* error, FAUser*user) {

            if (error != nil) {
                // Error
                NSLog(@"ERROR CODE: %d",error.code);
                NSLog(@"ERROR: %@",error);

                if (error.code == -9999) {

                    email.textColor = [UIColor redColor];

                }

            } else {

                NSLog(@"Created %@",user.userId);
                Firebase *userRef = [[Firebase alloc] initWithUrl:[NSString stringWithFormat:@"https://myapp.firebaseio.com/users/%@",displayName.text]];
                [[userRef childByAppendingPath:@"name"] setValue:fullName.text];
                [[userRef childByAppendingPath:@"user_id"] setValue:user.userId];

                Firebase *userCredentials = [[Firebase alloc] initWithUrl:[NSString stringWithFormat:@"https://myapp.firebaseio.com/users/%@/credentials",displayName.text]];
                [[userCredentials childByAppendingPath:@"email"] setValue:email.text];
                [[userCredentials childByAppendingPath:@"password"] setValue:password.text];



                AppDelegate *appDelegate= (AppDelegate *)[[UIApplication sharedApplication] delegate];

                [appDelegate.authClient loginWithEmail:email.text andPassword:password.text
                                   withCompletionBlock:^(NSError* error, FAUser* user) {

                                       if (error != nil) {
                                           NSLog(@"There was an error logging in to this account: %@",error);

                                       } else {
                                           NSLog(@"logged in");
                                           [self dismissViewControllerAnimated:NO completion:nil];

                                           [appDelegate.window.rootViewController presentViewController:appDelegate.tabBarController animated:YES completion:nil];

                                       }
                                   }];                    

            }

        }];

    }
    else {

        displayName.textColor = [UIColor redColor];

    }

    }

1 个答案:

答案 0 :(得分:2)

由于抛出异常,请将“createUserWithEmail”代码行抛到“@try{} / @catch{}”块中:

@try {
    [appDelegate.authClient createUserWithEmail:email.text password:password.text andCompletionBlock:^(NSError* error, FAUser*user) {
        ...
        ...
        // keep that big huge block part intact
        ...
        ...
    }
}
@catch (NSException * e) {
        NSLog(@"Exception: %@", e);
    }
}