2015-03-13 75 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