RestKit: change base URL of RKObjectManager

ios, ipad, iphone, restkit

Solution

Just spent a while figuring out how this can be done in v0.20. From what I can tell, you can't directly change the base URL without getting into the AFNetworking source code. You can create a new `HTTPClient` and set it, but I found that this caused even more problems, presumably because RestKit is doing some additional configuration on AFNetworking's `HTTPClient` when you setup the `RKObjectManager`, and by setting the client directly you are missing out on that.

I came up with this solution, which is to create another `RKObjectManager` with the new baseURL and re-add the descriptors. You'll also need to set your serialization and header types again.

NSString *urlString = @"http://www.something.com/api";
RKObjectManager *newManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:urlString]];
[newManager setRequestSerializationMIMEType:RKMIMETypeJSON];
[newManager setAcceptHeaderWithMIMEType:RKMIMETypeJSON];
[newManager addResponseDescriptorsFromArray:[RKObjectManager sharedManager].responseDescriptors];
[newManager addRequestDescriptorsFromArray:[RKObjectManager sharedManager].requestDescriptors];
[RKObjectManager setSharedManager:newManager];

Related documentation: Using Multiple Base URLs in RestKit

Problem

Is it possible to change base URL of `RKObjectManager` after creation? I have login box and from nickname I decide which URL for API I should use. If I create `RKObjectManager` after filling nick/password, I can make only single call from RestKit ( https://groups.google.com/forum/?fromgroups#!topic/restkit/wFNhpCW-URA ). If I create `RKObjectManager` `viewDidLoad` function - I cannot change URL. Is there same solution to my problem? Thanks.

Original source