Working with URLs (iOS 4)

With HTTP being the basis of most internet programming, it shouldn’t be a surprise that URLs are a foundational technique for internet-based programming. You’ll use them whether you’re calling up UIImageViews, accessing content by hand, or parsing XML. As a result, we’ll spend some time on the two fundamental URL classes: NSURL and NSURLRequest. We’ll also look at how to manipulate HTML data by hand.

Creating an NSURL

An NSURL is an object that contains a URL. It can reference a website or a local file, as any URL can. You’ve used it in the past to access Apple’s stock page and to load local media files for play.

As noted in the NSURL class reference, you can use numerous methods to create an NSURL. The most important ones are listed in table 14.1.

Table 14.1 A variety of NSURL creation methods

Method

Summary

fileURLWithPath:

Creates a URL from a local file path

URLWithString:

Creates a URL from a string; equivalent to

initWithString:

URLWithString:relativeToURL:

Adds a string to a base URL; equivalent to

initWithString: relativeToURL:

When you have an NSURL in hand, you can do any number of things with it:


■ You can pass it on to functions that require a bare NSURL.

■ You can query its properties to easily break down the URL into its parts. As usual, you can find a complete list of properties in the Apple reference, but properties like baseURL, fragment, host, path, port, and query may be particularly useful.

■ You can use the NSURL to load a UIWebView.

The first two possibilities require only the use of an NSURL; but when you’re working with a UIWebView, you must first create an NSURL and then turn it into an NSURLRequest.

NSURL and CFURLRef

NSURL is a toll-free bridge to CFURL, making an NSURL * and a CFURLRef equivalent. We take advantage of this in topic 12 when dealing with the MPMoviePlayer-Controller and with sounds. Whenever you need to create a CFURLRef, you can do so using the standard methods for NSURL creation that are described in this topic.

Building an NSURLRequest

The NSURLRequest class contains two parts: a URL and a specific policy for dealing with cached responses. As noted in table 14.2, there are four ways to create an NSURLRequest, although we expect you’ll usually fall back on the simple factory method, reques tWi thURL:.

Table 14.2 The related NSURLRequest init methods

Method

Summary

requestWithURL:

Creates a default request from the URL; equivalent to initWithURL:

requestWithURL:

cachePolicy:timeoutInterval:

Creates a request with specific caching choices; equivalent to initWithURL: cachePolicy:timeoutInterval:

By default, an NSURLRequest is built with a caching policy that’s dependent on the protocol and a timeout value of 60 seconds, which should be sufficient for most of your programming needs. If you need to get more specific about how things are loaded, you can call requestWithURL:cachePolicy:timeoutInterval:, giving it an NSURLRequestCachePolicy for the policy and an NSTimeInterval for the timeout.

You can also create a more interactive NSURLRequest by using the NSMutableURL-Request class, which allows you to more carefully form and modify the request that you’re sending. We’ll talk about this in section 14.6, when we examine how to send POST requests.

The NSURLRequest will get you through most web page work. As with the NSURL, you can do a few different things with an NSURLRequest. You can hand it off to a UIImageView, or you can use it to read in the contents of a web page, to later manipulate it by hand.

Manipulating HTML data by hand

To read the contents of a web page manually, you need to access an NSURLRequest’s properties. Table 14.3 lists some of the most important ones, although, as usual, you can find more information in the class reference.

Table 14.3 NSURLRequest can give access to a page’s content.

Property

Summary

allHTTPHeaderFields

Returns an NSDictionary of the header

HTTPBody

Returns an NSData with the body

valueforHTTPHeaderField:

Returns an NSString with the header

Other ways to read HTTP content

If you’re not reading data that meets the HTTP protocol, you can’t use NSURL-Request’s properties to access the data. Instead, you must fall back on other functions that let you read in data from an NSURL.

You’ve already met functions that read data that follows other protocol specifications, such as the MPMoviePlayerController and the sound players from topic 12. Similarly, in this topic we’ll talk about an XML parser. All of these classes can read directly from a URL.

If you need to capture raw data that isn’t set in HTML, the best way to do so is with an init or factory method that reads from a URL, such as NSData’s dataWith-ContentsOfURL:. We’ll look at an example of that in the last section of this topic.

The catch with these properties is that you can work only with well-defined HTML pages. Most notably, the NSURLRequest properties can’t read fragments, such as would be generated by Ajax or JSON, nor can they parse other sorts of content, such as XML or RSS.

You may also discover that you need a more interactive way to deal with HTML data. In this case, you’ll probably use an NSURLConnection object; but as with the NSMutableURLRequest, we’ll save that for later, because you’ll typically need to use it only when you’re POSTing information to a web page rather than just retrieving it.

For the moment, we’ll put all these complexities aside and look at how to display straight HTML data using the SDK’s UIWebView.

Next post:

Previous post: