URL Resources

Foundation has two classes that facilitate working with URLs: NSURL and NSURLHandle . NSURL represents a Uniform Resource Locator (URL). This class lets applications create, manipulate, and pick apart URLs. NSURLHandle accesses data and resources specified by an instance of NSURL. This class can access resources provided by HTTP, FTP,[5] and file services.

Working with URLs

NSURL represents a URL—the human-readable host names and paths that various network clients use to locate resources on the local filesystem or over the Internet. NSURL provides a number of methods and initializers that let you create instances in many different ways, as shown in Example 6-2.

Tip

Core Foundation has a type, CFURL, that is “toll-free bridged” to NSURL. As such, the two types can be used interchangeably. CFURL and NSURL are essentially equivalent: NSURL objects can be used in Core Foundation calls that ask for a CFURL, and vice versa. NSURL is just one of several Foundation classes that have a bridged Core Foundation equivalent.

Example 6-2. Creating and initializing instances of NSURL
                     // From a string...
NSURL *url = [NSURL URLWithString:@"http//www.macdevcenter.com"];
url = [[NSURL alloc] 
         initWithString: :@"http//www.macdevcenter.com"];

// From a file path...
url = [NSURL fileURLWithPath:@"/Users/mike/Pictures/pic.tiff"];
url = [[NSURL alloc] 
    initFileURLWithPath: :@"/Users/mike/Pictures/pic.tiff"];

// Access a URL with scheme, host, and path url = [[NSURL alloc] initWithScheme:@" ...

Get Cocoa in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.