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 video.
- [Instructor] Sometimes in your application you may want to respond to events that happen within the Safari View Controller. You can handle those events through delegation. Now we've already looked at delegation in the past. The process that we've looked at is, you'd say that the class is going to implement a certain delegate protocol by typing a comma and the protocol name, and then you implement the required methods and optional methods as you see fit. The process for a Safari View Controller is no different.
Let's take a look. After UIViewController on line 12, I'm going to say that this class implements the SFSafariViewControllerDelegate protocol. If you want to see which methods that that delegate protocol supports, click inside of there and then head over to Quick Help. In Quick Help, you'll see a link to open it up in Developer Documentation. Click that link, and then you can take a look at what the Safari View Controller Delegate does, and it's optional and required methods.
So we have did complete initial load, activity items for title, this tells the delegate that the user tapped an action button. We have Safari View Controller did finish, which tells the delegate that the user dismissed the view. So if you want to handle any of these, you can. Let's take a look at this one right here, Safari View Controller did finish. So, I'm going to close this and go back into our code, and I'm going to say that the Safari VC's delegate is self.
So it is this view controller class. So SafariVC.delegte equals self. And then I'm going to handle that delegate method. Below did press button, I'll handle Safari View Controller did finish, and inside of there, I'll just run a print statement. And since we want to see this show up right when the view controller is dismissed, let's get out of fullscreen mode.
So we can see the simulator and the debug area at the same time, and run the application. When the app loads up, hit the button. Then hit done. And you can see the finished text show up in the debug area. So, if you want to handle delegate methods for the Safari View Controller, implement the protocol, set the delegate property, and then handle whichever methods you'd like.
- Advanced UI controls
- Creating alert controllers
- Displaying web content
- Creating a collection view controller
- Creating a custom data type
- Creating tabbed apps
- Importing and setting icons for your apps
- Publishing your app to the App Store