我正在创建一个多用户 iPhone 应用程序,并且正在尝试完成用户登录过程中的编码。我可以成功创建一个帐户,并将用户输入的数据存储到 Core Data DB 中,并将 pin(密码)存储到 Keychain 中,所以现在我正在尝试完成登录过程。下面列出的以下代码是我到目前为止所拥有的,我想知道我需要做什么才能完成登录过程。
- (IBAction)processLogin:(id)sender {
// hide keyboard
[_textFieldUsername resignFirstResponder];
[_textFieldPin resignFirstResponder];
// First - make activity indicator visible, then start animating, then turn of wrong user / pin label
_welcomeActivityIndicator.hidden = FALSE;
[_welcomeActivityIndicator startAnimating];
[_wrongUserPin setHidden:YES];
// check if username and pin text fields are populated
if ([_textFieldUsername.text length ] == 0 && [_textFieldPin.text length ] == 0)
{
[_welcomeActivityIndicator stopAnimating];
[_wrongUserPin setHidden:NO];
}
// CORE DATA
NSFetchRequest *request= [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Account" inManagedObjectContext:_managedObjectContext];
NSPredicate *predicate =[NSPredicate predicateWithFormat:@"username=%@",self.textFieldUsername.text];
//check pin
Account *pinAccount = [[Account alloc] init];
[pinAccount.password isEqualToString:_textFieldPin.text];
[request setEntity:entity];
[request setPredicate:predicate];
NSError *error = nil;
NSArray *array = [_managedObjectContext executeFetchRequest:request error:&error];
if (array != nil) {
NSUInteger count = [array count]; // may be 0 if the object has been deleted.
NSLog(@"Username may exist, %@",count);
}
else {
NSLog(@"Username does not exist.");
}
// TODO - put this in proper place - play audio bell if user logs in correctly
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"Glass", CFSTR("aiff"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
// TODO - put this in proper place - Load ViewControllerHome
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
ViewControllerHome *home = (ViewControllerHome *)[storyboard instantiateViewControllerWithIdentifier:@"Home"];
[self presentModalViewController:home animated:YES];
}
Account和AccountBase类文件m和h如下所示:
Account.h http://pastie.org/4149299
Account.m http://pastie.org/4149296
AccountBase.h http://pastie.org/4149301
AccountBase.m http://pastie.org/4149302
我将不胜感激任何想法或想法,并感谢您的阅读。