2013-04-03 60 views
0

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

我在控制台得到的错误是:

*终止应用程序由于未捕获的异常 '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]; 

    } 

    } 
+0

有任何C ode或代码片断,表明您使用Firebase的*结果*进行操作?你还可以显示提取电子邮件地址文本字段文本的代码吗? – 2013-04-03 22:07:31

+0

这是我创建一个新用户的代码。 – 2013-04-03 22:11:58

回答

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); 
    } 
} 
+0

发现了这个异常,但我不知道如何处理这个错误。 例外:fjdksla; f是一个无效的电子邮件地址 2013-04-03 17:19:36.747板[11487:19703]参考 2013-04-03 22:23:02

+0

现在,如果snapshotValue或“Ref”= NULL,这意味着电子邮件没有采取用户可以注册。我遇到的问题是,如果“参考”为空,但电子邮件无效,则应用程序崩溃。 – 2013-04-03 22:25:38

+0

在那里,你可能会抛出一些“'UIAlertView'”或其他一些警报(例如,包括来自服务器的有关无效电子邮件地址的消息,这将使你的用户很好)。 – 2013-04-03 22:25:44