Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Because of the data that we're using comes from the network, we need to create a network connection and read that data before we can parse it and use it. For this, we'll use the NSURLConnection class. Start by making a working copy of BWRSS-XML-02, or in this case, the -done version. You can use your version from the previous lesson if you have it, which would not have this -done at the end. So, I'm going to make a working copy of this, and I'm going to rename it BWRSS-XML-03, and I'm going to go ahead and open the project by double-clicking on this Xcode project file.
We have a working table view here, so now it's time to set up the NSURLConnection class, so we can grab the feed from the network. We're going to start by importing the BWUtilities, because we're going to use that for our error displays. We're going to come back out here to Finder, and we're going to grab a bunch of codes from this 03-NSURLConnection.txt file. This just has a lot of stuff in it that we don't want to have to type right now. We're going to start by grabbing a bunch of these constants.
These constants are used for a number of purposes. We'll take a look at them in a moment here. Copy that and just going to paste all of that right in here at the top of the Source file. This should be right after these imports and right before this class extension for private members. So these constants are used throughout the code for a lot of different purposes, both for the URL connection and for the parsing code, and it's just that we don't have to type a lot of these literal strings and having them all in one place makes it easier to make changes later.
So, this is just good programming practice, to use constants instead of literals. So, I'm going to press Command+S to Save that and come back out here to my text file, and I'm going to grab everything from here all the way to the end. We're just going to paste this in, and we'll look at it as we go through it. So, I'm going to copy, and I'm going to come down here all the way to the end of our code and just before the end marker there, I'm going to paste this all in, and you can see here, starting with our Support methods, we have loadRSSFeed, which creates the NSURLRequest object, and it uses the URL from the feedRecord.
And then it sets up the connection with the delegate as the self, that allows us to use all of these NSURLConnection delegate methods. It sets our networkActivityIndicator that's the little spinner in the bar at the top of the screen on our iOS device. Once that's started, then the NSURLConnection calls these delegate methods, which are again very simple. Set up the connection, we didReceiveResponse, we set that networkActivityIndicatorVisible again, and we set up a place to store the data.
Every time we receive data, we go ahead and appendData that accumulates it in our NSMutableDate object. When we're done, we'll go ahead and detachNewThread to parse the data. Now, we don't have our parser code set up in here yet, so I'm going to comment that out and in the event of an error, I have this didFailWithError, we go ahead and call our error code. So, we have this handlError down here and the errorAlert, which uses the alertMessage method from the BWUtilities, or if we have more data to display, it goes ahead and creates a UIAlertView.
So, in order for this to work, we need to call all this code some place. So, we're going to come back up here to our viewDidLoad--and I'm just going to take out all of these comments here--and I'm going to go ahead and call our loadRSSFeed, self loadRSSFeed, and we will compile, I am pressing Command+B on my keyboard. Build Succeeded. Make sure the iPhone Simulator is selected up here and press Run.
Now when I click on this, you see a little network indicator came up, but we don't have any indication yet that anything is happening. You can see the network indicator kind of flash up there. So, let's go back here in the code, and I'm going to press Command+Period to stop the Simulator. And in our DidFinishLoading in the URLConnection delegate, you notice where I commented out this detachThread for calling our parser that we don't have yet, I'm going to put in an NSLog, so we can see that we got to this point, which means that we have finished loading the data, and we have data.
I'm just going to type have data in the NSLog, press Command+S and press the Run button, and now when I click on one of these, you see we get that have data down here. Just make a little more room for that, and I'll go ahead and click on another one, we have data, and I'll click on another one, we have data. We see our little network indicator coming up there on the Carrier Bar at the top of the Simulator. So, I'm going to switch back to Xcode. Press Command+Period, and we've stopped running the Simulator. Now, our NSURLConnection is complete.
You can see it's pretty simple to use or at least it's a bit demystified. The delegate classes tend to appear less intimidating over time as you get to know them. Now, we are successfully reading the feed using our NSURLConnection object, and we have RSS data that we're ready to parse for the Item View.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 100273 Viewers
56 Video lessons · 113240 Viewers
71 Video lessons · 82124 Viewers
131 Video lessons · 39411 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.