使用 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)setCompletionBlockWithSuccessvoid ( ^ ) ( RKObjectRequestOperation *operation , RKMappingResult *mappingResult ))success failurevoid ( ^ ) ( 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" objectperation];
if (failure) {
failure(operation, error);
}
}];
}
@end
然后注册你的子类:
[[RKObjectManager sharedManager] registerRequestOperationClass:[CustomRKObjectRequestOperation class]];
并监听您在上面发送的“connectionFailure”:
[[NSNotificationCenter defaultCenter] addObserver:self selectorselector(connectionFailedWithOperation name"connectionFailure" object:nil];
在监听器中(例如您的 AppDelegate 或登录管理器):
- (void)connectionFailedWithOperationNSNotification *)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/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |