从另一个类的方法中获取NSURLConnection响应(从帮助器类) [英] Get NSURLConnection response (from a helper class) inside method of a different class

查看:87
本文介绍了从另一个类的方法中获取NSURLConnection响应(从帮助器类)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个"WebAPI"类,可以处理所有Web API调用,该类通过其基于异步委托的调用使用NSURLConnection.

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

每当对象需要与Web API通信时,它将使用WebAPI的实例并调用所需的方法,如下所示:在登录的情况下,我从AppDelegate进行以下调用:

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:

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

问题在于,一旦执行performLoginWithUserName:andPassword调用,代码便会继续进行,并且在WebAPI.m中实现的委托方法中会收到任何/所有响应.

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.

这是一个实际的问题,因为我需要能够获取响应代码和在类方法中从WebAPI调用发起的地方接收到的所有数据.我希望能够做到这一点:

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

其中WebAPIResponse类是一个自定义类,它将包含HTTP状态代码和接收到的所有数据.

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

如果我将WebAPI.m更改为使用NSURLConnection sendSynchronousRequest,则可以实现,但这不能使我接收所有HTTP代码.

This is achievable if I change WebAPI.m to use NSURLConnection sendSynchronousRequest, but that doesnt enable me to receive all HTTP codes.

满足此要求的最佳方法是什么?

What would be the best way to fulfill this requirement?

谢谢您的帮助.

推荐答案

您可以使用块来处理响应. 例如:

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

}

这篇关于从另一个类的方法中获取NSURLConnection响应(从帮助器类)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆