How to Parse an NSURL Object

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

Comments are closed.