Tuesday, April 30, 2013

NSURL URLWithString relativeToURL example ios


URLWithString :relativeToURL:

Creates and returns an NSURL object initialized with a base URL and a relative string.
+ (id)URLWithString:(NSString *)URLString relativeToURL:(NSURL *)baseURL
Parameters of [NSURL URLWithString]
URLString
The string with which to initialize the NSURL object. May not be nil. Must conform to RFC 2396. URLString is interpreted relative to baseURL.
baseURL
The base URL for the NSURL object.
Return Value
An NSURL object initialized with URLString and baseURL. If URLString was malformed, returns nil.
Discussion of [NSURL URLWithString]
This method expects URLString to contain any necessary percent escape codes.
Example of [NSURL URLWithString]
- (void)testCreatingQueryURLfromBaseURL
{
    NSURL *url = [NSURL URLWithString:@"http://foo.com/bar/baz"];

    url = [NSURL URLWithString:@"?x=1&y=2&z=3" relativeToURL:url];

    STAssertEqualObjects([url absoluteString], @"http://foo.com/bar/baz?x=1&y=2&z=3", nil);
}
Example of [NSURL URLWithString]
NSURL *newsUrl = [NSURL URLWithString:@"/Document/News/1593" relativeToURL:[NSURL URLWithString:@"http://exist.ru"]]: