Reputation: 36178
I have a delegate on a SFSafariViewController
that does clean up tasks in the safariViewControllerDidFinish
event. In iOS 13+, the user is able to swipe down to dismiss, but this event is not fired in that case. I do not want to disable this gesture by enabling isModalInPresentation
.
Is there a way to make the safariViewControllerDidFinish
fire for this case, or how can I detect the user swiping down to dismiss the Safari view controller?
Upvotes: 4
Views: 2768
Reputation: 460
@Frankenstein's answer worked for me, but it took me a while to setup my code correctly, so just want to add some code snippets here
class MyViewController: UIViewController {
func presentSafariVC() {
let url = //...
let config = SFSafariViewController.Configuration()
let safariVC = SFSafariViewController(url: url, configuration: config)
safariVC.modalPresentationStyle = .formSheet
safariVC.delegate = self
safariVC.presentationController?.delegate = self
present(safariVC, animated: true)
}
}
extension MyViewController: SFSafariViewControllerDelegate, UIAdaptivePresentationControllerDelegate {
// click top left "Done" button and dismissed
func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
// do your stuff
}
// swipe down and dismissed
func presentationControllerDidDismiss(_ presentationController: UIPresentationController) {
// do your stuff
}
}
Also, I was originally looking for a way to prevent swipe down to dismiss the presented vc, but it seems setting safariVC.isModalInPresentation = true
has no effect, I can still swipe down to dismiss it. This is observed on XCode 12, iOS 14.2 simulator.
Upvotes: 2
Reputation: 16371
You can know when the user dismisses the SFSafariViewController
by swiping down by making yourself the presentation controller's delegate and overriding presentationControllerDidDismiss(_:)
. Check out the documentation on this topic.
Note: This method will not get called if you use dismiss(animated:)
programmatically.
Upvotes: 2