Safari view controllers allow you to open a Safari browser window in your application without having to leave your app. In this tutorial, see why Safari view controllers are useful and how to implement them.
This causes the user to not leave your application but stay within it. Next, you can actually change applications and send the user to either Safari or another web browser installed on their device. Finally, you could use something called a WK web view and create your own web browser within your application. So we're going to be talking about all these three different methods inside of this chapter. I want to start with talking about the Safari view controller.
The first step in implementing a Safari view controller is to import Safari services, which you can do underneath the import statement for UIKit. My application here is pretty simple. There's just a button on the screen in my storyboard that's connected to some code that runs this method showWebContent. So inside of here, we're going to create our Safari view controller and present that to the user. The first thing we're going to do is create a URL. So let url equal, we'll create a new URL, we're going to create it from a string, so I'll put the URL of some website in here.
For now, I'm just going to use the Google home page. You can use anything you want. And creating a URL gives you an optional value, so what we want to do is put this inside of an if statement, if let url equals URL, and then some curly braces after it. So if this is not an optional value, so if creating this URL indeed does work, first we'll have to add dot com after Google, then we run the code inside of the curly braces.
So inside of here we're going to create our Safari view controller and present it to the user. Let safariVC equal SFSafariViewController. So instead of the UI prefix, it has SF for Safari. Open up the parentheses and we're going to initialize it with a URL, and that will be the URL that we created earlier, so we want the one that's all lowercase. And then finally we present this to the user with the present method.
We've used the present method a few times so far in this course. I'll have it be animated, and for completion I'll put nil. This is going to be called when the user is presented with the Safari view controller. So typically you're not going to handle this method, but it's there if you ever need to use it. So let's run the application and see what we get. Hit the show link button.
You'll see Safari open up, and it should open that link after just a second or two. And there it is. We can now search Google and look at all the cat pictures that you want. To dismiss it, you can hit the done button, but notice that you can refresh the page. You have back and forward buttons, share button, and a view in Safari button that opens up the full Safari application.
When you hit the Done button, the Safari view controller is dismissed and you're back in your application. So if you want to use a Safari view controller, import SafariServices, then simply create a URL, a Safari view controller, and then present the Safari view controller.
- Alert controllers and actions
- Sliders, progress bars, and switches
- Creating UI elements with code
- Displaying web content with Safari view controllers and WKWebView
- Creating collection view controllers
- Creating tabbed apps
- Publishing your app with App Store Connect