1

I'm new to iPhone application development, but I seem to have somewhat managed so far (learning as I go).. however, I've run in to an issue I can't figure out. Here's the scenario:

I have an extension class called CoreAPI which I have my network functions inside. I have a function called "Login" inside CoreAPI which makes a request to a server, gets the 0 or 1 response and returns true or false. I had this working perfectly with Synchronous requests, but now I need to make everything asynchronous.

So, below is what I'm trying to do.. this is not working with the error along the lines of "Object of type ID has no method loginCallback"

loginViewController.m

- (void) login {
    CoreAPI APIObject = [[CoreAPI alloc] init];
    [APIObject login:self];
}

- (void) loginCallback {
    NSLog(@"It called back.");
}

CoreAPI.m

- (void)login:(id)sender {
    [sender loginCallback];
}

Thanks, and please let me know if I have missed any details needed to help me.

Sebastian
  • 7,670
  • 5
  • 38
  • 50
DanielM8
  • 15
  • 3

2 Answers2

1

I'm seeing a couple problems. First, I'm guessing you don't provide visibility of the loginCallback function to CoreAPI.m. You should be able to send any message to an id type, so long as it's defined for a known type.

However, I'd recommend using a completion block in this case. Here's what that could look like:

(in CoreAPI.h)

- (void)login:(void (^)(void))completed;

(in CoreAPI.m)

- (void)login:(void (^)(void))completed {
    // Login code
    if (completed) completed();
}

Calling it would look like:

CoreAPI APIObject = [[CoreAPI alloc] init];
[APIObject login:^{
     NSLog(@"It called back.");
}];

They have really goofy syntax, but blocks are really nice for this sort of thing. Hope this helped! Let me know if I didn't explain something clearly.

oltman
  • 1,772
  • 1
  • 14
  • 24
0

this should do the trick for you:

first, import loginViewController header inside CoreApi.m

#import "loginViewController.h"

Then, change login method to this:

- (void)login:(id)sender {
    [(loginViewController*)sender loginCallback];
}

Or this:

- (void)login:(loginViewController*)sender {
        [sender loginCallback];
}

Explanation: notice that your login method is receiving by parameter one object of type id . In objective C, id type means a reference to any Objective-C of unknow class. So, inside your login method, the compiler doesn't know that the sender is a instance of your loginViewController class, so it won't recognize loginViewController's methods.

To informations about this, please read: Objective-C: difference between id and void *

Notice that I only focused in remove your actual error. You should have to do more things in order to accomplish your code to run asynchronous.

In order to perform a better callback, please look for delegates or blocks (like in oltman's answer).

To run things in background, look for CDG : http://developer.apple.com/library/ios/#DOCUMENTATION/General/Conceptual/ConcurrencyProgrammingGuide/OperationQueues/OperationQueues.html

Community
  • 1
  • 1
Lucas Eduardo
  • 11,525
  • 5
  • 44
  • 49