When working with NSURL objects, often times there are a number of values passed along with the URL. For example, a query string is often included as a means to embed HTML form data. This tip shows how to parse and print the values of a URL.
The code below begins by defining a URL and is followed by logging the url values to the debug console:
NSURL *url = [NSURL URLWithString: @"http://some-site.com:999/dir1/dir2;param?field-1=value-1&field-2=value-2#anchor1"]; NSLog(@"Scheme: %@", [url scheme]); NSLog(@"Host: %@", [url host]); NSLog(@"Port: %@", [url port]); NSLog(@"Path: %@", [url path]); NSLog(@"Relative path: %@", [url relativePath]); NSLog(@"Path components as array: %@", [url pathComponents]); NSLog(@"Parameter string: %@", [url parameterString]); NSLog(@"Query: %@", [url query]); NSLog(@"Fragment: %@", [url fragment]);
The output is shown below: