Question

I want to show a login dialogue and login error dialogue if necessary.

I use UIAlertView to show these dialogues, but the process keep running while showing the UIAlertView.

I wrote a code below. Now NSUserDefaults doesn't keep those value, so I expected login dialogue is shown and wait until button to be tapped.

But when run this, error dialogue is shown and after tapping OK for this, login dialogue is shown.

How can I fix this?

Thanks in advance.

- (void)storeEvernote
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    evID = [defaults stringForKey:@"evID"];
    evPW = [defaults stringForKey:@"evPW"];
    NSLog(@"%@", evID);

    if( evID == NULL || evPW == NULL )
    {
        login = true;
        [self showLoginDialogue];
    }
    else
    {
        evernoteID = evID;
        evernotePW = evPW;
    }
    if( evernoteID == NULL || evernotePW == NULL )
    {
        login = false;
        [self showErrMessage];
        return;
    }
    [self getEvernoteNotebooks];
}

- (void)showLoginDialogue
{
    UIAlertView *loginDialogue = [[UIAlertView alloc] initWithTitle:@"Evernote Login"     message:@"Enter your Evernote Info" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Login", nil];
    loginDialogue.delegate = self;
    loginDialogue.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;

    [loginDialogue show];
}

- (void)showErrMessage
{
    UIAlertView *loginalert = [[UIAlertView alloc] initWithTitle:@"Login Failure" message:@"Invalid ID & Password" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [loginalert show];
}
Was it helpful?

Solution

The first time that storeEvernote method runs, evID and evPW are NULL so the first if is true, it shows the LoginDialoge then it continues to second if and because evernoteID and evernotePW are still NULL so the condition in second if statement is also true, so it shows the errorMessage. To fix this, move the second if statement to delegate method of the loginDialogue ALSO ADD return; to the end of first if statement for example:

- (void)storeEvernote
{

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    evID = [defaults stringForKey:@"evID"];
    evPW = [defaults stringForKey:@"evPW"];
    NSLog(@"%@", evID);
if( evID == NULL || evPW == NULL )
{
    login = true;
    [self showLoginDialogue];
    return;
}
else
{
    evernoteID = evID;
    evernotePW = evPW;
}

[self getEvernoteNotebooks];
}

//The login dialoge delegate method:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
   //Process user input
      ...
   if( evernoteID == NULL || evernotePW == NULL )
    {
        login = false;
        [self showErrMessage];
        return;
    }
    [self getEvernoteNotebooks];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top