iOS登录过程适用于模拟器但不适用于iPhone(3GS)

时间:2012-07-08 20:52:28

标签: ios core-data keychain

我为我正在开发的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存储在keychain中。我可以在iPhone上创建一个帐户,并且信息场景显示iPhone上已创建帐户,但当我尝试登录时,我看到的是用户名或密码错误

该项目可以从这里下载https://github.com/ipatch/KegCop/zipball/master

2 个答案:

答案 0 :(得分:0)

看起来您在此代码中的任何位置都没有存储用户名/密码到CoreData。你确定你的默认数据库中有条目吗?

答案 1 :(得分:0)

我不确定是什么解决了这个问题,但在完成following tutorial之后,登录过程又重新开始了。