【问题标题】:Validating emails with firebase for iOS使用 iOS 版 Firebase 验证电子邮件
【发布时间】:2013-03-25 19:21:38
【问题描述】:

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

    }

    }

【问题讨论】:

  • 是否有任何代码或 sn-p 表明您使用 Firebase 的 result?能否也显示提取电子邮件地址文本字段文本的代码?
  • 这是我创建新用户的代码。

标签: ios email-validation firebase


【解决方案1】:

由于抛出异常,请将“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);
    }
}

【讨论】:

  • 捕获了异常,但我不知道如何处理错误。例外:fjdksla;f 是无效的电子邮件地址 2013-04-03 17:19:36.747 Plate[11487:19703] Ref
  • 现在如果snapshotValue或"Ref" = NULL,这意味着电子邮件没有被接收,所以用户可以注册。我遇到的问题是,如果“Ref”为 NULL 但电子邮件无效,则应用程序崩溃。
  • 在那里你可能会抛出一些“UIAlertView”或其他一些警报(例如,包括来自服务器的关于无效电子邮件地址的消息,这对你的用户来说会很好) .
  • 哦,好吧,现在说得通了。我想我没有想清楚。我可以处理@catch 语句中的错误。非常感谢您的帮助,迈克尔!
  • 嘿,对不起。无效的电子邮件不应使应用程序崩溃。我们将在该库的未来版本中修复此问题(可能在本月晚些时候)。请关注 @FirebaseRelease,了解有关 Firebase 库新版本的公告。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-28
  • 2017-05-30
  • 2020-07-16
相关资源
最近更新 更多