问题描述
我正在使用AFHTTPSessionManager在网络上进行api调用。我有会话管理器的signleton对象,它会一次初始化基本url。有时,我需要使用不同的baseurl进行api调用。
在这里处理不同的baseurl的正确方法是什么?
+(ApiClient *)sharedClient {
静态ApiClient * _sharedClient = nil;
static dispatch_once_t OncePredicate;
dispatch_once(& oncePredicate,^ {
_sharedClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:kTraktBaseURLString]];
});
return _sharedClient;
}
您可以使用以下行为 + URLWithString:relativeToURL:
方法来覆盖baseURL。
Matt
因此,如果您要更改单个请求的baseURL,可以将绝对URL作为 URLString
参数传递给 GET:parameters:success:failure:
而不是URL路径。
[管理员GET:@ http://otherBaseURL.com/url/path参数:nil成功:...失败:...]
I am using AFHTTPSessionManager to make api calls on the web. I have signleton object of session manager and it initiates the base url once. Occasionally, I am in a need of making api call with different baseurl. And its readonly in AFNetworking.h file.
What is the proper way of handing different baseurl here? Plese help.
+ (ApiClient *)sharedClient {
static ApiClient *_sharedClient = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
_sharedClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:kTraktBaseURLString]];
});
return _sharedClient;
}
You can use the behaviour of +URLWithString:relativeToURL:
method to override baseURL.
Matt mentioned it in Docs
So if you want to alter baseURL for single request, you can pass Absolute URL as a URLString
argument to GET:parameters:success:failure:
instead of URL path.
[manager GET:@"http://otherBaseURL.com/url/path" parameters:nil success:... failure:...]
这篇关于使用两个不同的baseUrl-AFHTTPSessionManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!