O'Reilly logo

Cocoa in a Nutshell by James Duncan Davidson, Michael Beam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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:@" ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required