本文介绍了如何使用AFIncrementalStore在每个请求中添加Auth令牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个iOS + Rails 3.1应用程序,并且正在使用 AFIncrementalStore 进行客户端-服务器通信。

I have an iOS + Rails 3.1 app, and I'm using AFIncrementalStore for the client-server communication.

我已经实现了根据此教程在我的Rails服务器上进行令牌身份验证:

I have implemented Token Authentication on my Rails server according to this tutorial: http://matteomelani.wordpress.com/2011/10/17/authentication-for-mobile-devices/

我现在要包含<$ c客户端到服务器的每个请求中的$ c>& auth_token = XXXXXXXX ,包括POST请求。我该怎么做?我在以下相关文章中找不到解决方案:中,应该指出正确的方向。

UPDATE: I skimmed your question (sorry!), and my sample code below works for a regular AFHTTPClient, but not AFIncrementalStore. The same basic approach will work, though, and there's sample code at this answer that should point you in the right direction.

您不能只将& auth_token =任何内容附加到

您可能想覆盖 getPath ... postPath ... 方法,类似于:

You probably want to override your getPath... and postPath... methods with something like:

- (void)getPath:(NSString *)path
     parameters:(NSDictionary *)parameters
        success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
        failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
    if (parameters) {
        // Make a mutable copy and add the "token" parameter to the dictionary
        NSMutableDictionary *mutableParams = [parameters mutableCopy];
        [mutableParams setObject:@"whatever" forKey:@"token"];
        parameters = [NSDictionary dictionaryWithDictionary:mutableParams];
    } else {
        parameters = @{@"token" : @"whatever"};
    }

    [super getPath:path parameters:parameters success:success failure:failure];
}

此方法将允许AFNetworking根据您的特定请求和

This approach will allow AFNetworking to appropriately encode your parameters depending on your specific request and encoding settings.

如果您要滚动自己的 AFHTTPRequestOperation 对象而不是使用便捷方法(您可能不是) t),只需确保将令牌包含在参数 之前,然后按如下方式创建NSURLRequest:

If you are rolling your own AFHTTPRequestOperation objects instead of using the convenience methods (you probably aren't), just make sure you include the token in parameters before you create your NSURLRequest like so:

NSURLRequest *request = [self requestWithMethod:@"GET" path:path parameters:parameters];

这篇关于如何使用AFIncrementalStore在每个请求中添加Auth令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-14 06:01