Thursday, June 26, 2014

xcode core data (database) access - search -login

- (BOOL) userLogin:(NSString *)username
                  :(NSString *)password
{
   
    
    NSFetchRequest *userFetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"User"];
    userFetchRequest.predicate = [NSPredicate predicateWithFormat:@"username == %@", username];
    NSError *error;
    NSArray *users = [self.context executeFetchRequest:userFetchRequest error:&error];
    NSManagedObject *matches = nil;
    if ([users count] == 0) {
        NSLog(@"Invalid Username");
        return FALSE;
    } else {
        matches = [users objectAtIndex:0];
        NSLog([NSString stringWithFormat:@"%@",users]);
        NSLog([matches valueForKey:@"username"]);
        NSLog([matches valueForKey:@"password"]);
        NSString *mypass =[matches valueForKey:@"password"];
        if ([mypass isEqualToString:password]) {
            NSLog(@"All correct at login");
            return TRUE;
        }
        else
        {
            NSLog(@"Invalid Password");
            return FALSE;
        }
        
        
       // NSLog([NSString stringWithFormat:@"%d matches found", [objects count]];

    }

No comments:

Post a Comment