I would like to take a moment to apologize to you, Drew, if you are reading this post, because we had a discussion the other day about what we both need to be focusing on, and I have definitely allocated a few crucial hours towards a different focus, and I’d like to talk about a few topics from this new project. I’d like to discuss a few delegate methods of the NSURLConnection class and there applications.

NSURLConnection has a very useful delegate method called connection:didReceiveResponse: which is capable of providing the developer with an instance of a very useful class called NSURLResponse. I encourage you to look at the documentation for this class, because it can provide some very useful information about the file you are trying to load across an NSURLConnection. Two very cool properties of this class are suggestedFilename and expectedContentLength. With suggestedFilename we can determine the type of the file we are trying to load, then decide what to do with it from there. Let’s say, for example, your user enters “http://tumblr.com” in a UITextField you’ve created which triggers a method that creates a NSURLRequest object with the initializer: [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:textField.text]]; If we load this request into an NSURLConnection object and start the request, once the object calls this delegate method upon response of the URL, the suggestedFilename property of the NSURLResponse object will be something like: “tumblr.com.html,” which is cool, because we can now determine from that string that the file that the user is trying to load is of the type “HTML.”

So if you’re brain is working a little faster than you are reading, you’ve probably already thought about the gravity and potential of this knowledge. If your user wants to load something other than an HTML or PHP file, we can use methods to display those in a way that is more appealing to the user. For example, if the file is a JPEG, you could download that file and add it to a queue of other images that are stored locally to display in a photo album. Or if the file is an MP3, why not download that file and add it to a local playlist, or even stream the file using AirPlay? Get creative. There are a couple of different methods you’ll want to acquaint yourself with. If all you want to do is load the HTML file into a UIWebView, you could use the method: [webView loadRequest:request]; where request is the NSURLRequest object we allocated earlier, and webView is your instance of UIWebView. If you do load the web page this way, make sure to call: [connection cancel]; where connection is the NSURLConnection object passed by connection:didReceiveResponse:. You’ll also want to look at the NSURLConnection delegate method connection:didReceiveData:. You can use the suggestedFilename property I mentioned earlier, as well as an NSMutableData object and its property length to calculate the progress of a download by using [theData appendData:data]; where theData is your NSMutableData object and data is the NSData object passed by this delegate method.

Good luck, and feel free to ask me something at http://daltonclaybrook.tumblr.com/ask

1 year ago