Get an NSURLConnection response (from a helper class) inside a method of another class

I have a "WebAPI" class that handles all web API calls, the class uses NSURLConnection through its delegate-based asynchronous calls.

Whenever an object needs to interact with the web API, it will use the WebAPI instance and call the required method, as shown below, in case of login. I make the following call from AppDelegate:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
     WebAPI *webAPI = [[WebAPI alloc] init];
     [webAPI performLoginWithUserName:@"test1@myserver.com" andPassword:@"password"];
}

The problem is that after calling executeLoginWithUserName: andPassword, the code progresses, and any / all response is received in the delegate methods that are implemented in WebAPI.m.

This is a real problem, because I need to be able to get response codes and any data received in the class method where the call came from in WebAPI. I would like to be able to:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
     WebAPI *webAPI = [[WebAPI alloc] init];
     WebAPIResponse * webAPIRespnse = [webAPI performLoginWithUserName:@"test1@myserver.com" andPassword:@"password"];
}

Where the WebAPIResponse class is a custom class that will contain the HTTP status code and any received data.

This can be done if I change WebAPI.m to use NSURLConnection sendSynchronousRequest, but this does not allow me to receive all HTTP codes.

What would be the best way to fulfill this requirement?

Thank you for your help.

+4
source share
2 answers

You can use blocks to handle responses. For instance:

WebApi.h
- (void)performLoginWithUsername:(NSString *)userName 
                     andPassword:(NSString *)password
                    successBlock:(void(^)(NSData *response))successBlock
                    failureBlock:(void(^)(NSError *error))failureBlock;

WebApi.m
@interface WebAPI()
    @property (nonatomic, copy) void(^authorizationSuccessBlock)(NSData *response);
    @property (nonatomic, copy) void(^authorizationFailureBlock)(NSError *error);
@end

@implementation WebAPI
- (void)performLoginWithUsername:(NSString *)userName 
                     andPassword:(NSString *)password
                    successBlock:(void(^)(NSData *response))successBlock
                    failureBlock:(void(^)(NSError *error))failureBlock {
    self.authorizationSuccessBlock = successBlock;
    self.authorizationFailureBlock = failureBlock;
    // NSURLConnection call for authorization here
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    if (self.authorizationSuccessBlock != nil) {
        self.authorizationSuccessBlock(data);
        self.authorizationSuccessBlock = nil;
    }
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    if (self.authorizationFailureBlock != nil) {
        self.authorizationFailureBlock(error);
        self.authorizationFailureBlock = nil;
    }
}

AppDelegate.m
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
   WebAPI *webAPI = [[WebAPI alloc] init];
   [webAPI performLoginWithUserName:@"test1@myserver.com" andPassword:@"password" successBlock:^(NSData *response) {
      // Handle result here
   } failureBlock:^(NSError *error) {
      // Handle error here
   }];

}

+2
source

WebAPI, , , .

0

Source: https://habr.com/ru/post/1536422/


All Articles