Now, we have a working webView and we'd like to be able to pass pages off to Safari as a full-featured mobile web browser. Let's see how we do that. We'll navigate here to Chapter 6 of our exercise files, and we're going to make a working copy of our webView-02. I'm using the -done version. You can use the version that you created if you've been following along with the exercises. I'm going to rename that to -03 and open it up in Xcode by double-clicking on the Xcode project file here in the project folder.
So, I'm going to minimize that and come back up here to our actionButton method. And this our actionButton that gets called whenever we press that button. It has an NSLog right now. So, if I run this in the iPhone Simulator, and I'll bring up a page here, and you'll notice when I press this button, instead of launching Safari, it just displays this log message that says the URL that's been called.
And so I'll come back here in Xcode. I'm going to press Command+. to stop the simulator. I'm going to minimize that down there, and we're just going to replace this line with the line of code that will actually call Safari. And that looks like this. It's a method call on an object. So, I'm going to type in UIApplication.sharedApplication and openURL. And for the URL itself, I'm going to call self.webView.request.URL.
And so the webView itself keeps a copy of the URL for the current request that is being displayed. And so that gives us the URL. This openURL method is called on UIApplication.sharedApplication. So, UIApplication is a centralized point of control for all iOS applications. It provides access to system resources. sharedApplication is a property that gives the application instance, the individual application that you are using. So, you call methods on the sharedApplication property within UIApplication.
openURL is the system entry point for opening a URL resource with the associated application. Our HTTP or HTTPS applications will open in Safari. So, let's go ahead and run this in the Simulator, pressing Command+S to save and clicking on the run button here. And now, when I bring this web page up and I press this button here, it will load up Safari with that URL.
So now, we have a complete working webView. It's really a very simple task. Cocoa Touch provides a simple delegate-based webView that's powerful, flexible, and easy to implement, and it works very well.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98566 Viewers
61 Video lessons · 85854 Viewers
71 Video lessons · 69749 Viewers
56 Video lessons · 102056 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.