gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:47:05 25 4
gpt4 key购买 nike

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

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

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

最佳答案

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

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

关于ios - Restkit - 无论如何要在收到 403 响应时设置默认操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15857566/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com