0

I'm VERY new to Objective C and iOS development (like 5 hours new :-). I've got some code that calls an API to authenticate a user and returns a simple OK or FAIL. I can get the result to write to the console but what I need to do is get that result as part of my IBAction.

Here's the IBAction code:

- (IBAction) authenticateUser
{
    [txtEmail resignFirstResponder];
    [txtPassword resignFirstResponder];

    [self performAuthentication];

    if (authResult == @"OK")

What I need is for authResult to be the JSON result (OK or FAIL). Here is the code that gets the result:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [connection release];

    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    NSLog(@"%@", responseString);
    [responseData release];

    NSMutableDictionary *jsonResult = [responseString JSONValue];
        if (jsonResult != nil) 
            {
                NSString *jsonResponse = [jsonResult objectForKey:@"Result"];
                NSLog(@"%@", jsonResponse);
            }
}

Thank you so much for any help and sorry if I'm missing something obvious!

1 Answer 1

1

I'm a little confused as to what's going on here... it looks like your -performAuthentication method must start an asynchronous network request via NSURLConnection, and your connection's delegate's -connectionDidFinishLoading: gets to determine the result of the request. So good so far? But your -authenticateUser method expects authResult to be determined as soon as -performAuthentication returns. If the network request is asynchronous, that's not going to happen. If I'm following you, I think you need to do the following:

  1. Fix up -connectionDidFinishLoading: so that it actually sets authResult based on the Result value in jsonResponse. I'm sure you'd get around to this at some point anyway.
  2. Change -authenticateUser such that it doesn't expect to have an answer immediately. You've got to give the network request a chance to do its thing.
  3. Add another method, possibly called -authenticationDidFinish or something along those lines. Everything currently in -authenticateUser from the 'if (authResult...' to the end goes in this new method.
  4. Call the new method from -connectionDidFinishLoading:.
  5. Fix your string comparison. If you want to compare two strings in Cocoa, you say (for example):

    if ([authResult isEqualToString:@"OK") { }

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the reply! You got it exactly - that's what I'm trying to do and the Async network request is what I'm doing. OK - what you said makes total sense to me. The only thing I don't understand how to do is set the value for authResult. If I set it within the void function, I can't access it anywhere else in the code..?
I assumed that authResult was an member variable of whatever class implements all these methods, so you'd be able to set it in one method and use that value in any other. I'm glad to hear that it worked out... Keep at it -- it gets to be a lot of fun.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.