我为我正在开发的 iPhone 应用程序创建了一个登录过程,登录过程在 Xcode 中的 iOS 模拟器应用程序中工作,但在我的实际手机上不起作用。登录过程的代码是:
- (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];
// set entity for request
[request setEntity:entity];
// filter results using a predicate
NSPredicate *pred =[NSPredicate predicateWithFormat:(@"username = %@"), _textFieldUsername.text];
// set predicate for the request
[request setPredicate:pred];
NSError *error = nil;
// store DB usernames in results array
NSArray *results = [_managedObjectContext executeFetchRequest:request error:&error];
NSLog(@"The returned results are %@",results);
// check text field against results stored in DB
for (Account *anAccount in results) {
if ([anAccount.username isEqualToString:_textFieldUsername.text]){
NSLog(@"Your username exists");
if ([anAccount.password isEqualToString:_textFieldPin.text]){
NSLog(@"Your pin is correct");
// 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 ViewController(Root)Home
if([anAccount.username isEqualToString:@"root"])
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
ViewControllerRootHome *roothome = (ViewControllerRootHome *)[storyboard instantiateViewControllerWithIdentifier:@"rootHome"];
[self presentModalViewController:roothome animated:YES];
}
else {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
ViewControllerHome *home = (ViewControllerHome *)[storyboard instantiateViewControllerWithIdentifier:@"Home"];
[self presentModalViewController:home animated:YES];
}
}
else {
NSLog(@"Your pin is wrong");
[_welcomeActivityIndicator stopAnimating];
[_wrongUserPin setHidden:NO];
}
}
else {
NSLog(@"Your username was not found");
[_welcomeActivityIndicator stopAnimating];
[_wrongUserPin setHidden:NO];
}
}
}
我将用户名存储在 Core Data DB 中,并将 pin 存储在钥匙串中。我可以在 iPhone 上创建一个帐户,并且信息场景显示 iPhone 上已创建帐户,但是当我尝试登录时,我看到的只是用户名或密码错误