Domanda

Ho un Tabbar / Tableview App che modale carichi una vista Login / Iscriviti quando i carichi app, mi hanno istituito un Root.plist in un impostazioni bundle per il nome e la password e hanno recuperato correttamente gli elementi. Voglio essere in grado di fare due cose:

1) Fare un test per vedere se le stringhe NSUserDefault sono vuote e in tal caso caricare la visualizzazione Login / iscrizione.

2) se le stringhe sono disponibili quindi utilizzare il contenuto della stringa per accedere al mio servizio Web.

Grazie in anticipo.

Ecco la mia 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];
È stato utile?

Soluzione

non voglio memorizzare username e password direttamente in Label.text, ma questo è il vostro programma. Per di controllo vuota provare questo:

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

Inoltre si potrebbe provare stringForKey: kUsernameKey invece di objectForKey. Infine assicurarsi che ad un certo punto che il nome utente e la password non sono spazi bianchi solo, ora presumo che hai fatto che il check-in prima di salvare NSUserDefaults.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top