At this point, we have a working WebView, and it's being called from the Item View when the user selects an item. Now, let's fill in the details in the class. Let's make a working copy of our webView-01, and you can use your results from the last exercise, or I'm going to use the done version. You can do that too as well. I'll rename this webView-02 and open it in Xcode by double-clicking on the Xcode project file. And we're going to back out here to Finder and grab these methods in this methods.txt file.
I'm just going to copy everything from right up there all the way down to the end of that section and come into Xcode and just place that down here before the end. And we can take a look at these. You'll notice the viewDidLoad has a little red exclamation point. That's because it's a duplicate. There was a stub already in here, so I'm just going to delete that stub and actually get rid of our little red bang there.
So, viewDidLoad is called the after the view is loaded and so it sets the title with the title from the feedItem, and it does some scaling and then it loads the request in the webView, and you notice it's using the webView property which in the .h file is actually an IBoutlet. So, that's already been hooked up in our storyboard. So, that will load the request, and it's going to want to call back the delegate methods, and we'll load those up here in a minute. ViewDidUnload, we just clear out our properties.
When view is loaded again later, that just prevents the same web page from showing up again, which probably won't happen anymore anyway. Some earlier versions of iOS, this was necessary. viewWillAppear, it sets the delegate for the webView to ourselves. And viewWillDisappear, it stops loading if the webView is still loading content, and it clears out the delegate. It also sets the networkActivityIndicator to off. Now, let's come back out here to TextWrangler and load up the WebViewDelegate methods.
So, whatever the document title is set to in the document itself, that will become the title. So initially, we loaded the title up here from our feedItem title, and now after the page is loaded, we update the title to whatever the document title is. And in the event there is an error, we have a little error document here which is just literal strings, which says that we had an error fetching the web page, and it fills it in with the localized description from the error object.
So, that's all really straightforward, and I'm going to save all of this, and we're going to go ahead and run it in the simulator. Make sure the iPhone Simulator is selected and press the Run button. Now, we'll just go ahead and load something from the lynda blog here. And you see, there is our networkActivityIndicator, and here is the document. You see, we have our reload button. If I press that, it reloads the page. There's our network activity spinner, and we have a stop button. So, if I start the reload and press stop right away, it stops it.
We have our action button, which still isn't hooked up. That will just display a log entry. And you notice these back and forward buttons. Those are grayed out. I can't actually press them. They're inactive but if I were to click on a link inside of here--there's our networkActivityIndicator-- we've brought up a new page. So, sometimes the back button doesn't light up, even though it's supposed to, and if you press the reload button, it makes it work. And this is just a bug in the webView. So now I can press this back button, and you see there's our networkActivityIndicator, and it's going back to the previous page.
And now, the forward button is lit up because that is now active, and it's now possible for that to work. So, if I press that, we'll go forward. I press that, we go back. So now, we have a working webview. Again, using the Cocoa Touch classes has made this very easy. Most RSS feed items point to a web page, so this solution makes it easier for us to display that rich data without a lot of coding. While our webView does display the content nicely and has a few useful features for navigation, it's not a full web browser.
So, we have a button for passing control of the content to Safari. When I press this button, of course right now all we got is this log entry. So, our remaining task is to implement that button.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101512 Viewers
61 Video lessons · 88272 Viewers
71 Video lessons · 72145 Viewers
56 Video lessons · 103904 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.
Your file was successfully uploaded.