Safari view controllers have delegate methods to handle when a Safari URL opens, or when the Safari view controller closes and returns control to your application. See how it works in this tutorial.
- [Instructor] As we discussed earlier, the present method allows you to present a view controller. You could optionally handle the completion event, to respond once that view controller has been presented. In normal situations, this is not something that you're going to handle. Typically you're more interested in when the view controller is dismissed. For example, if you send up a SafariViewController to your user, and your user uses that for whatever they want to do. They hit the done button, you may want to know, when that SafariViewController is dismissed.
You can handle that using delegation. Remember delegation involves two steps. The first step, is to say that your class implements, a particular delegate protocol. Let's do that now. So on line 12 where we have the class declaration, I'll type a comma after UIView Controller, and specify that this class implements the SFSafariViewControllerDelegate protocol. That's means it can be a delegate for a SafariViewController, so that's step one.
Step two is to tell the view controller, or whatever object you want to be a delegate of, that this class is going to serve as its delegate. To do that find where we create the SafariViewController, in this case it's on line 21. Go to the next line, tell the Safari View Controller that its delegate is self, so set its delegate property equal to self, that's saying that self or this class, rather this instance of this class, is going to handle the delegation for the SafariViewController.
So whenever there is an event in the SafariViewController, it can be handled by this view controller class. So go down a few lines below Show Web Content, and what you want to do is implement, one of the delegate methods. To do that start typing the word Safari. You'll see a list here like, SafariViewController Did Finish, SafariViewController Initial Load Did Redirect To URL, et cetera. For now we just want to handle SafariViewController Did Finish.
So handle that, and in here we all we're going to do is, just print out a message. So just print out the string finished, and I want to run the app outside of full screen mode so we can see the simulator and this message pop up at the same time. With all of this set up let's run the application and see what we get. There's our little app with show link. Hit the button, you should see the SafariViewController come up and then just hit done right away and watch for that method in the output window, and there it is, finished.
So now we can respond when the user dismisses the SafariViewController. I'll stop the app and return to Xcode. I'll also return to full screen mode. I want to point out here that there are other methods that you could implement using the SafariViewControllerDelegate protocol. To learn about those put your cursor in SFSafariViewControllerDelegate and open up the quick help menu which you can find in the right area of the screen. You can read more information about it here and click the link to open this up in developer documentation.
This brings us to SFSafariViewControllerDelegate, where you can read which methods you can handle and what they do. Now I'll close the documentation, so remember if you want to handle when the SafariViewController is dismissed, or any other delegate methods, you need to implement the protocol, and you need to set the View Controller's delegate to self.
- 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