Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
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