iOS中的登录功能

时间:2015-03-13 07:03:32

标签: ios objective-c

我正在尝试实现登录功能。我正在使用核心数据来实现这一目标,并且能够成功注册新用户。用户名和密码等详细信息存储在实体中。我现在想要将这些值与登录详细信息页面中的用户输入进行比较。

以下是注册的代码。

//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];
    }
}

问题是每当我尝试登录时,我都会收到“用户名/密码不正确”的提醒视图。

2 个答案:

答案 0 :(得分:0)

arrayFetch中有多少个对象?您正在CoreData中为所有Person条目发出请求,因此您拥有所有这些条目。我建议你用谓词提出请求

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

如果您收到任何结果,请使用该人员检查密码。

答案 1 :(得分: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。基本错误!