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.
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
Here is the way to change the RestKit baseURL
after init:
[RKObjectManager sharedManager].client.baseURL = [RKURL URLWithString:newBaseUrlString];
Notice from RestKit doc:
Changing the baseURL has the side-effect of causing the requestCache instance to be rebuilt. Caches are maintained a per-host basis.
I use it and it works fine :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With