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"]){
UIAlert开发者_如何学运维View *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Congrats" message:@"You are authorized "
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alertsuccess show];
[alertsuccess release];
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.
精彩评论