Work with two different basic commands - AFHTTPSessionManager

I use AFHTTPSessionManager to make api calls on the web. I have a session manager signleton object and it initiates the base url once. Sometimes I need to make an api call with different baseurl. And its only in the file AFNetworking.h.

What is the proper way to pass different baseurl here? Help Plesha.

+ (ApiClient *)sharedClient {

    static ApiClient *_sharedClient = nil;

    static dispatch_once_t oncePredicate;

    dispatch_once(&oncePredicate, ^{
        _sharedClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:kTraktBaseURLString]];
    });
    return _sharedClient;
}
+4
source share
1 answer

You can use the method behavior +URLWithString:relativeToURL:to override baseURL.

Matt mentioned this in Docs

HTTP- URL- baseURL, NSURL + URLWithString: relativeToURL:, . baseURL , NSURL, NSURL + URLWithString:.

baseURL :

NSURL *baseURL = [NSURL URLWithString:@"http://example.com/v1/"];
[NSURL URLWithString:@"foo" relativeToURL:baseURL];                  //  http://example.com/v1/foo
[NSURL URLWithString:@"foo?bar=baz" relativeToURL:baseURL];          // http://example.com/v1/foo?bar=baz
[NSURL URLWithString:@"/foo" relativeToURL:baseURL];                 // http://example.com/foo
[NSURL URLWithString:@"foo/" relativeToURL:baseURL];                 // http://example.com/v1/foo
[NSURL URLWithString:@"/foo/" relativeToURL:baseURL];                // http://example.com/foo/
[NSURL URLWithString:@"http://example2.com/" relativeToURL:baseURL]; // http://example2.com/

, baseURL , URL URLString GET:parameters:success:failure: URL-.

[manager GET:@"http://otherBaseURL.com/url/path" parameters:nil success:... failure:...]
+5

Source: https://habr.com/ru/post/1548437/


All Articles