Verifying existence of name and password in NSUserDefaults to Skip a login/Screen

Posted by Michael Robinson on Stack Overflow See other posts from Stack Overflow or by Michael Robinson
Published on 2010-06-04T17:46:23Z Indexed on 2010/06/07 4:12 UTC
Read the original article Hit count: 328

I have a Tabbar/Tableview App that modally loads a Login/Signup view when the app loads, I have set up a Root.plist in a settings bundle for the name and password and have successfully retrieved the items. I want to be able to do two things:

1) Do a test to see if the NSUserDefault Strings are empty and if so load the Login/Signup view.

2) If the strings are available then use the string contents to login to my Webservice.

Thanks in advance.

Here is my LoginViewController .m :

@synthesize usernameField;
@synthesize passwordField;
@synthesize loginButton;
@synthesize loginIndicator;
@synthesize usernameLabel;
@synthesize passwordLabel;





-(void)refreshFields {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    usernameLabel.text = [defaults objectForKey:kUsernameKey];
    passwordLabel.text = [defaults objectForKey:kPasswordKey];

}
- (void)viewDidAppear:(BOOL)animated {
    [self refreshFields];
    [super viewDidAppear:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self refreshFields];

[self.navigationController setNavigationBarHidden:YES animated:NO];
}



- (IBAction) login: (id) sender
{

    {

 NSString *post =[NSString stringWithFormat:@"username=%@&password=%@",usernameField.text, passwordField.text];     


   NSString *hostStr = @"http:~iphone_login.php?";
    hostStr = [hostStr stringByAppendingString:post];
    NSData *dataURL =  [NSData dataWithContentsOfURL: [ NSURL URLWithString: hostStr ]];    
    NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];

NSLog(@"Site: %@",hostStr); 
NSLog(@"Site: %@",serverOutput);    



if([serverOutput isEqualToString:@"Yes"]){


    UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Congrats" message:@"You are authorized "
                                                              delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

        [alertsuccess show];
        [alertsuccess release];

© Stack Overflow or respective owner

Related posts about iphone

Related posts about login