views:

184

answers:

1

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];
A: 

I would NOT store username and password directly into label.text, but this is your program. For empty check try this:

if ([usernameLabel.text length] == 0 || [passwordLabel.text length] == 0)
    [self loadLoginView];
else
    [self login:nil];

Also you could try stringForKey:kUsernameKey instead of objectForKey. Finally make sure at some point that your username and password are not whitespace only, now I presume you did that check before saving into NSUserDefaults.

JOM
The test works but I'm unsure about the commands,Does the [self loadLoginView] load my LoginView.xib? I'm looking for it to continue loading the view. Does the [self login] run the -(IBAction) login: (id sender ? I'm getting errors from stating the "the ViewController may not respond" to both of those commands.
Michael Robinson
Should have been [self login:nil], sorry. [self loadLoginView] is just dummy placeholder for your own function, which should load your LoginView.xib file. Usually it comes with LoginView.m and LoginView.h files, so it's not enough to "just" load xib file.
JOM
Thanks, All this was in my LoginView.m class, The LoginView controller is launched modally from the AppDelegate.m file. do you have I idea where this should go?
Michael Robinson
I asked a new question that continues where we left off.
Michael Robinson