如何使用AFIncrementalStore在每个请求中添加Auth令牌? [英] How to add an Auth Token in every request using AFIncrementalStore?

查看:100
本文介绍了如何使用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服务器上进行令牌身份验证: http: //matteomelani.wordpress.com/2011/10/17/authentication-for-mobile-devices/

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请求。我该怎么做?我在以下相关文章中找不到解决方案:使用带有Auth令牌的AFIncrementalStore a>

I now want to include the &auth_token=XXXXXXXX in every request from client to server, including POST requests. How would I do that? I haven't found the solution in this related post: Using AFIncrementalStore with an Auth token

更新:这是我第一次尝试代码,但似乎没有发送 auth_token

UPDATE: this is my first code attempt, but doesn't seem to send the auth_token:

(在我的 AFIncrementalStoreHTTPClient 子类内部)

- (NSMutableURLRequest *)requestForFetchRequest:(NSFetchRequest *)fetchRequest withContext:(NSManagedObjectContext *)context {
    NSMutableURLRequest *request = [[super requestForFetchRequest:fetchRequest withContext:context] mutableCopy];
    NSMutableString *requestBody = [[NSMutableString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding];
    [requestBody appendFormat:@"&%@=%@", @"auth_token", @"xkT2eqqdoNp5y4vQy7xA"];
    [request setHTTPBody:[requestBody dataUsingEncoding:NSUTF8StringEncoding]];
    return request;
}


推荐答案

更新:我略过了您的问题(对不起!),下面的示例代码适用于常规AFHTTPClient,但不适用于AFIncrementalStore。但是,相同的基本方法也可以使用,并且在此答案的示例代码中,应该指出正确的方向。

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令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆