简体   繁体   English

从另一个类的方法中获取NSURLConnection响应(来自辅助类)

[英]Get NSURLConnection response (from a helper class) inside method of a different class

I have a class, "WebAPI", that handles all web API calls, the class uses NSURLConnection through its asynchronous delegate-based calls. 我有一个类“WebAPI”,它处理所有Web API调用,该类通过其基于异步委托的调用使用NSURLConnection。

Whenever an object needs to communicate with the web API it will use an instance of WebAPI and call the required method as shown below in the case of signing in I make the folowing call from the AppDelegate: 每当一个对象需要与Web API通信时,它将使用一个WebAPI实例并在登录的情况下调用所需的方法,如下所示,我从AppDelegate进行以下调用:

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

The problem is that once the performLoginWithUserName:andPassword call is made, the code progresses on and any/all response is received in the delegate methods that are implemented in WebAPI.m. 问题是,一旦执行了performLoginWithUserName:andPassword调用,代码就会继续进行,并且在WebAPI.m中实现的委托方法中会收到任何/所有响应。

This is a real issue because I need to be able to get response codes and any data received within the class method from where the call to the WebAPI, originated . 这是一个真正的问题,因为我需要能够获取响应代码以及在类方法中接收到的来自WebAPI调用的所有数据。 I would like to be able to this : 我希望能够:

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

Where WebAPIResponse class is a custom class that will contain the HTTP Status code and any data that is received. 其中WebAPIResponse类是一个自定义类,它将包含HTTP状态代码和接收的任何数据。

This is achievable if I change WebAPI.m to use NSURLConnection sendSynchronousRequest, but that doesnt enable me to receive all HTTP codes. 如果我将WebAPI.m更改为使用NSURLConnection sendSynchronousRequest,这是可以实现的,但这并不能使我接收所有HTTP代码。

What would be the best way to fulfill this requirement? 什么是满足这一要求的最佳方式?

Thank you for your help. 谢谢您的帮助。

You could use blocks to handle responses. 您可以使用块来处理响应。 For example: 例如:

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
   }];

} }

更改WebAPI类以提供其自己的委托接口,或者在请求中使用完成块,这些块在异步连接完成时调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM