Search code examples
iosurlhyperlinknsurlconnectionrelative-path

NSURLConnection with links relatively to parent?


For example, I have absolute link:

http://stackoverflow.com/questions/ask

and relative link:

../../users

So the "summary link will be (link1):

http://stackoverflow.com/questions/ask/../../users

Which refers to the same address as the following one (link2):

http://stackoverflow.com/users

The problem is it seems NSURLConnection doesn't understand link1 and understands link2 only. Could you provide the way how to transform link1 -> link2? Ot maybe better solution except of replacing NSURLConnection with something else?


Solution

  • NSURLConnection just requests a URL, which must be a complete URL, not a URL fragment. What you need to do is something like this:

    NSURL *baseURL = ...
    NSString *relativePath = @"../../users";
    NSURL *finalURL = [NSURL URLWithString:relativePath relativeToURL:baseURL];
    

    and then make the request using that URL. (I think that you do need to first make sure the link text isn't an absolute URL, but it might work even in that case.)