0

我为我正在开发的 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 上已创建帐户,但是当我尝试登录时,我看到的只是用户名或密码错误

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

4

2 回答 2

0

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

于 2012-07-08T21:21:44.830 回答
0

我不确定是什么解决了这个问题,但在完成以下教程后,登录过程又开始了。

于 2012-07-20T03:14:57.743 回答