Verifying existence of name and password in NSUserDefaults to Skip a login/Screen
- by Michael Robinson
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];