嗨,我有以下代码访问URL:

NSString * stringURL = [NSString stringWithFormat:@"%@/%@/someAPI", kSERVICE_URL, kSERVICE_VERSION];
NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:stringURL]];

AFJSONRequestOperation * operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    completionHandler(JSON, nil);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
    completionHandler(nil, error);
}];

但我想将用户 token 作为HEADER的参数传递给X-USER-TOKEN

无法在AFNetworking documentation上找到它,我应该更改操作类型吗?

最佳答案

使用AFHTTPClient或将其子类化!

您可以使用-setDefaultHeader:value:设置默认 header ,如下所示:

[self setDefaultHeader:@"X-USER-TOKEN" value:userToken];

您可以检查documentation

关于ios - 如何在JSONRequestOperation上向AFNetworking添加自定义 header ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15141115/

10-09 02:07