0

提交创建新用户的请求时,如果用户尝试使用无效电子邮件提交,则应用程序崩溃(IE 没有@符号)。有没有办法通过 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];

    }

    }
4

1 回答 1

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);
    }
}
于 2013-04-03T22:17:02.140 回答