2013-04-03 57 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