Showing posts with label URLWithString. Show all posts
Showing posts with label URLWithString. Show all posts

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"]]:

NSURL URLWithString example ios


URLWithString:

Creates and returns an NSURL object initialized with a provided string.
+ (id)URLWithString:(NSString *)URLString
Parameters of [NSURL URLWithString]
URLString
The string with which to initialize the NSURL object. Must be a URL that conforms to RFC 2396. This method parses URLString according to RFCs 1738 and 1808. (To create NSURL objects for file system paths, use fileURLWithPath:isDirectory: instead.)
Return Value
An NSURL object initialized with URLString. If the string was malformed, returns nil.
Discussion of [NSURL URLWithString]
This method expects URLString to contain any necessary percent escape codes, which are ‘:’, ‘/’, ‘%’, ‘#’, ‘;’, and ‘@’. Note that ‘%’ escapes are translated via UTF-8.
Example of [NSURL URLWithString] - 1
NSString *myString = @"http://foo.bar/?key[]=value[]<>";
NSURL *myUrl = [NSURL URLWithString:[myString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
Example of [NSURL URLWithString] - 2
//localisationName is a arbitrary string here
NSString* webName = [localisationName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
NSString* stringURL = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@,Montréal,Communauté-Urbaine-de-Montréal,Québec,Canadae&output=csv&oe=utf8&sensor=false", webName];
NSString* webStringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL* url = [NSURL URLWithString:webStringURL];