Search code examples
ioscore-datakeychain

iOS Login process works on simulator but not iPhone (3GS)


I created a login process for an iPhone app I am developing, and the login process works in the iOS Simulator app in Xcode, but doesn't work on my actual phone. The code for the login process is:

- (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];
        }
    }

}

I am storing the username in a Core Data DB, and storing the pin in the keychain. I am able to create an account on the iPhone, and the info scene shows that there are created accounts on the iPhone, but when I try to login all I see is Username or pin is wrong

The project can be downloaded from here https://github.com/ipatch/KegCop/zipball/master


Solution

  • I am not exactly sure what solved this problem, but after completing the following tutorial, the login process was working again.