Restkit - 无论如何在收到403响应时设置默认操作?

时间:2013-04-07 00:33:25

标签: ios restkit

使用Restkit,我在AppDelegate中设置了RKObjectManager,一切正常。我想知道是否有某种方法可以为特定的响应代码设置默认操作。

例如,用户使用我的iPhone应用程序登录我的api并返回使用auth_token。如果在任何时候,对于任何请求,我回来我得到403响应(如果auth_token到期)我想将RootViewController更改为我的登录屏幕。

在我的应用中设置此功能的最佳方式是什么?

2 个答案:

答案 0 :(得分:3)

在RestKit 0.20中,您可以注册RKObjectRequestOperation,这样您就可以通过自己的成功/失败块传递所有请求/响应。

http://blog.higgsboson.tk/2013/09/03/global-request-management-with-restkit/

#import "RKObjectRequestOperation.h"

@interface CustomRKObjectRequestOperation : RKObjectRequestOperation

@end

@implementation CustomRKObjectRequestOperation

- (void)setCompletionBlockWithSuccess:(void ( ^ ) ( RKObjectRequestOperation *operation , RKMappingResult *mappingResult ))success failure:(void ( ^ ) ( RKObjectRequestOperation *operation , NSError *error ))failure
{
    [super setCompletionBlockWithSuccess:^void(RKObjectRequestOperation *operation , RKMappingResult *mappingResult) {
        if (success) {
            success(operation, mappingResult);
        }

    }failure:^void(RKObjectRequestOperation *operation , NSError *error) {

        [[NSNotificationCenter defaultCenter] postNotificationName:@"connectionFailure" object:operation];

        if (failure) {
            failure(operation, error);
        }

    }];
} 

@end

然后注册您的子类:

[[RKObjectManager sharedManager] registerRequestOperationClass:[CustomRKObjectRequestOperation class]];

并倾听" connectionFailure"你在上面发送:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(connectionFailedWithOperation:) name:@"connectionFailure" object:nil];

在监听器中(例如您的AppDelegate或登录管理器):

- (void)connectionFailedWithOperation:(NSNotification *)notification
{
    RKObjectRequestOperation *operation = (RKObjectRequestOperation *)notification.object;
    if (operation) {

        NSInteger statusCode = operation.HTTPRequestOperation.response.statusCode;

        switch (statusCode) {
            case 0: // No internet connection
            {
            }
                break;
            case  401: // not authenticated
            {
            }
                break;

            default:
            {
            }
                break;
        }
    }
}

答案 1 :(得分:1)

使用RestKit 0.10时,您可以使用给定的委托方法objectLoaderDidLoadUnexpectedResponse

- (void)objectLoaderDidLoadUnexpectedResponse:(RKObjectLoader *)objectLoader {
    if ([[objectLoader response] statusCode] == 403) {
        // Your action here
    }
}

在RestKit 0.20中,您可以对单个代码或一组代码使用响应描述符。

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping 
                                                                                   pathPattern:nil 
                                                                                       keyPath:@"yourKeyPath" 
                                                                                   statusCodes:[NSIndexSet indexSetWithIndex:403]];

documentation中的更多状态代码集。

更新

使用BaseViewController处理其他一个视图控制器中发出的请求错误时,您可以设置通知。

BaseViewController

- (void)viewDidLoad 
{
    // ...

    // Set observer for notification e.g. "requestFailedWith403Error"
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handle403Error:) name:@"requestFailedWith403Error" object:self];
}

- (void)handle403Error:(NSNotification)notification 
{
    // Code for handling the error 
}

SubViewController

- (void)loginToServer 
{
    // ...

    // Set authorization header
    [[RKObjectManager sharedManager].HTTPClient setAuthorizationHeaderWithUsername:@"username" password:@"password"];

    // e.g. POST to server
    [[RKObjectManager sharedManager] postObject:yourObject
                                       path:@"path/toserver"
                                 parameters:nil
                                    success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                                        // Handling success
                                    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
                                        // Handling error with notification
                                        [[NSNotificationCenter defaultCenter] postNotificationName:@"requestFailedWith403Error" object:self];
    }];
}

通过处理错误来优化您的中心配置,您可以再看一下RestKit Wiki中给出的示例代码(添加了错误映射)。