Search code examples
iosxcodeafnetworking-2nsurl

NSURL with string relative to URL returning unexpected result


This is the call I am making:

NSURL *url = [NSURL URLWithString:@"rooms" relativeToURL:[NSURL URLWithString:@"localhost:9000"]];

and the result of url.absoluteURL is:

localhost:///rooms

does this make sense? I was expecting the result to be localhost:9000/rooms

Thanks!


Solution

  • You are missing a scheme. NSURL seems to interpret the localhost: part as scheme, which causes unexpected behaviour. In case of http you should try this:

    NSURL *url = [NSURL URLWithString:@"rooms" relativeToURL:[NSURL URLWithString:@"http://localhost:9000/"]];