我正在嘗試實現登錄功能。我正在使用核心數據來實現這一點,並能夠成功註冊一個新用戶。用戶名和密碼等詳細信息存儲在實體中。我現在想要將這些值與登錄詳細信息頁面中的用戶輸入進行比較。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];
}
}
問題是,我每次嘗試登錄時都會收到「錯誤的用戶名/密碼」警報視圖。
Ameen,請確保您的文本字段中沒有空格,在我的情況下,由於自動在文本字段中添加了額外空間(Intellisence),因此發生了一些錯誤 – magid 2015-03-13 14:28:35
第二次首先記錄arrayFetch的值,然後記錄謂詞的值。這會讓你清楚。現場背後發生了什麼 – magid 2015-03-13 14:29:50
我做了NSLog,但是這個值是空的.....它不是顯示空,它只是沒有顯示任何東西。通過查看代碼,你知道問題是什麼嗎? – 2015-03-13 14:46:22