2

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

例如,用户使用我的 iPhone 应用程序登录到我的 api 并获取 auth_token 以重新使用。如果在任何时候,对于任何请求,我都会收到 403 响应(例如 auth_token 是否过期),我想将 RootViewController 更改为我的登录屏幕。

在我的应用程序中进行设置的最佳方法是什么?

4

2 回答 2

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;
        }
    }
}
于 2014-06-05T09:08:53.537 回答
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]];

文档中的更多状态代码集。

更新

当使用 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 
}

子视图控制器

- (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中给出的示例代码(其中添加了错误映射)。

于 2013-04-08T17:37:18.940 回答