2015-03-13 79 views
0

我正在嘗試實現登錄功能。我正在使用核心數據來實現這一點,並能夠成功註冊一個新用戶。用戶名和密碼等詳細信息存儲在實體中。我現在想要將這些值與登錄詳細信息頁面中的用戶輸入進行比較。iOS中的登錄功能

以下是註冊碼。

//first we grab the managed obj context 
     managedObjectContext = self.managedObjectContext; 

     //creating an instance 
     //getting the entity in which the data is to be stored and store the new obj with the data in it 
     NSManagedObject *pplAcc = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:managedObjectContext]; 

     //populating the obj with the data stored in the text fields 
     [pplAcc setValue:nameTxt.text forKey:@"name"]; 
     [pplAcc setValue:paswordTxt.text forKey:@"password"]; 
     [pplAcc setValue:radioLbl forKey:@"radio"]; 

     //saving the img in binary format 
     NSData *imgData = UIImagePNGRepresentation(profImg.image); 
     [pplAcc setValue:imgData forKey:@"image"]; 

     //saving 
     NSError *error; 
     if (![managedObjectContext save:&error]) { 
      NSLog(@"Error in saving %@", [error localizedDescription]); 
     } 
     else{ 
      NSLog(@"User registration successful"); 
      LoginViewController *lvc = [self.storyboard instantiateViewControllerWithIdentifier:@"login"]; 
      [self.navigationController pushViewController:lvc animated:YES]; 
     } 

這裏是登錄的代碼。

-(IBAction)btnLogin:(id)sender{ 

    managedObjectContext = self.managedObjectContext; 

    //fetching the result 
    NSFetchRequest *fetch = [[NSFetchRequest alloc]initWithEntityName:@"Person"]; 


NSPredicate *namePredicate = [NSPredicate predicateWithFormat:@"name = %@",nameTxt.text]; 
    NSPredicate *passwordPredicate = [NSPredicate predicateWithFormat:@"password = %@", passwordTxt.text]; 
    NSCompoundPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[namePredicate, passwordPredicate]]; 

    [fetch setPredicate:compoundPredicate]; 

    NSError *error = nil; 
    //excuting the fetch result 
    NSArray *arrayFetch = [managedObjectContext executeFetchRequest:fetch error:&error]; 

    //checking to see if the user input equals the fetched managedobj in coredata 
    if (
     [nameTxt.text isEqual:[arrayFetch valueForKey:@"name"]] && 
     [passwordTxt.text isEqual:[arrayFetch valueForKeyPath:@"password"]] && 
     [radioLbl isEqual:[arrayFetch valueForKey:@"radio"]] 
     ) { 
     if ([radioLbl isEqual: @"Student"]) { 
      StudentViewController *student = [self.storyboard instantiateViewControllerWithIdentifier:@"student"]; 
      [self.navigationController pushViewController:student animated:YES]; 
     } 
     else if ([radioLbl isEqual: @"Instructor"]){ 
      InstructorViewController *instructor = [self.storyboard instantiateViewControllerWithIdentifier:@"instructor"]; 
      [self.navigationController pushViewController:instructor animated:YES]; 
     } 
    } 
    else{ 
     UIAlertView *error = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Incorrect username/password" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
     [error show]; 
    } 
} 

問題是,我每次嘗試登錄時都會收到「錯誤的用戶名/密碼」警報視圖。

+0

Ameen,請確保您的文本字段中沒有空格,在我的情況下,由於自動在文本字段中添加了額外空間(Intellisence),因此發生了一些錯誤 – magid 2015-03-13 14:28:35

+0

第二次首先記錄arrayFetch的值,然後記錄謂詞的值。這會讓你清楚。現場背後發生了什麼 – magid 2015-03-13 14:29:50

+0

我做了NSLog,但是這個值是空的.....它不是顯示空,它只是沒有顯示任何東西。通過查看代碼,你知道問題是什麼嗎? – 2015-03-13 14:46:22

回答

0

我已經解決了這個問題,我在這裏發佈更新的代碼給需要幫助的其他人。這些更改是在登錄代碼中進行的。

-(IBAction)btnLogin:(id)sender{ 


    managedObjectContext = self.managedObjectContext; 

    //fetching the result 
    NSFetchRequest *fetch = [[NSFetchRequest alloc]initWithEntityName:@"Person"]; 


    NSPredicate *namePredicate = [NSPredicate predicateWithFormat:@"name = %@",nameTxt.text]; 
    NSPredicate *passwordPredicate = [NSPredicate predicateWithFormat:@"password = %@", passwordTxt.text]; 
    NSCompoundPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[namePredicate, passwordPredicate]]; 

    [fetch setPredicate:compoundPredicate]; 

    NSManagedObject *object = nil; 
    NSError *error = nil; 
    //excuting the fetch result 
    NSArray *arrayFetch = [managedObjectContext executeFetchRequest:fetch error:&error]; 

if ([arrayFetch count] > 0) { 

     object = [arrayFetch objectAtIndex:0]; 
     //checking to see if the user input equals the fetched managedobj in coredata 
     if ([nameTxt.text isEqualToString:[object valueForKey:@"name"]] || [passwordTxt.text isEqualToString:[object valueForKey:@"password"]]){ 
      if ([radioLbl isEqualToString:@"Student"]) { 

       StudentViewController *student = [self.storyboard instantiateViewControllerWithIdentifier:@"student"]; 
       [self.navigationController pushViewController:student animated:YES]; 
      } 
      else{ 
       InstructorViewController *instructor = [self.storyboard instantiateViewControllerWithIdentifier:@"instructor"]; 
       [self.navigationController pushViewController:instructor animated:YES]; 
      } 

      nameTxt.text = @""; 
      passwordTxt.text = @""; 

     } 
    } 
    else{ 
     UIAlertView *error = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Incorrect username/password" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
     [error show]; 
    } 
} 

我在上面的代碼中缺少的是NSManagedObject。基本錯誤!

0

arrayFetch中有多少個對象? Your're在CoreData中爲所有人員條目提出請求,以便您擁有所有人員。我建議你用一個謂詞做出請求

NSPredicate *predicate = [NSPredicate predicatewithformat:"name = %@",nameTxt.text]; 

如果你得到任何結果,然後檢查那個人的密碼。

+0

我已經更新我的答案使用NSpredicate如您所建議的,但它仍然顯示「不正確的用戶名/密碼」。我也添加了用戶註冊碼。但對於登錄只需要名稱和密碼 – 2015-03-13 11:10:16