ios - Restkit - 无论如何要在收到 403 响应时设置默认操作?
<p><p>使用 Restkit,我在 AppDelegate 中设置了 RKObjectManager,一切正常。我想知道是否有某种方法可以为特定响应代码设置默认操作。</p>
<p>例如,用户使用我的 iPhone 应用程序登录到我的 api 并获取 auth_token 以供使用。如果在任何时候,对于任何请求,我都会收到 403 响应(例如 auth_token 是否过期),我想将 RootViewController 更改为我的登录屏幕。 </p>
<p>在我的应用中进行此设置的最佳方式是什么?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>在 RestKit 0.20 中,您可以注册您的 <code>RKObjectRequestOperation</code>,这样您就可以先通过自己的成功/失败 block 传递所有请求/响应。</p>
<p> <a href="http://blog.higgsboson.tk/2013/09/03/global-request-management-with-restkit/" rel="noreferrer noopener nofollow">http://blog.higgsboson.tk/2013/09/03/global-request-management-with-restkit/</a> </p>
<pre><code>#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) {
[ postNotificationName:@"connectionFailure" object:operation];
if (failure) {
failure(operation, error);
}
}];
}
@end
</code></pre>
<p>然后注册你的子类:</p>
<pre><code>[ registerRequestOperationClass:];
</code></pre>
<p>并监听您在上面发送的“connectionFailure”:</p>
<pre><code>[ addObserver:self selector:@selector(connectionFailedWithOperation:) name:@"connectionFailure" object:nil];
</code></pre>
<p>在监听器中(例如您的 AppDelegate 或登录管理器):</p>
<pre><code>- (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;
case401: // not authenticated
{
}
break;
default:
{
}
break;
}
}
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - Restkit - 无论如何要在收到 403 响应时设置默认操作?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/15857566/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/15857566/
</a>
</p>
页:
[1]