Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closes SFSafariViewController on a certain url

I am trying to close SFSafariViewController when I reach a certain page.

But I am unable to do it until the "Done" button is pressed explicitly by the user.

What I want is to get the URL as soon as it reached a certain page and then dismiss the view controller. Then I can pick the rest using this

func safariViewControllerDidFinish(_ controller: SFSafariViewController){
    // do work here
}

Note: This question has been asked before but I was unable to find any satisfactory answers

like image 745
Moaz Khan Avatar asked Nov 13 '17 08:11

Moaz Khan


3 Answers

Maybe It's late But I think It will help anyone search for the same question Listening to the SFSafariViewControllerDelegate delegate you can use this method inside

func safariViewController(_ controller: SFSafariViewController, initialLoadDidRedirectTo URL: URL) {}

inside it you can make if condition on your certain page you need to take action for ... as example if I want to close it when reaching certain page I'will fo this

 func safariViewController(_ controller: SFSafariViewController, initialLoadDidRedirectTo URL: URL) {
    if URL.absoluteString == "WHATEVER YOUR LINK IS"{
        controller.dismiss(animated: true, completion: nil)
    }
}
like image 72
abdallah Nader Avatar answered Oct 13 '22 21:10

abdallah Nader


If you register a URI scheme for your app, you could have have a link on a page which takes you back to your app. At that point you'll be in

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {

of your AppDelegate and you can do whatever you wish.

“Because since Safari View Controller has access to a user’s credentials, synced across all of their devices with iCloud Keychain, logging in is going to be a breeze.[…] It takes two steps. The first is where you would’ve used your own in-app browser, just present an instance of SFSafariViewController.

And once the user is finished logging in and the third-party web service redirects back to your app with the custom URL scheme that you fed it, you can accept that in your AppDelegate‘s handleOpenURL method.

From there you can inspect the response and dismiss the instance of SFSafariViewController because you know that the authentication is done.

That’s it. Two steps.”

Source: http://asciiwwdc.com/2015/sessions/504#t=1558.026

Please note the handleOpenURL method is deprecated in favor of application:openURL:options:.

like image 28
DBD Avatar answered Oct 13 '22 21:10

DBD


You cannot do that with a SFSafariViewController. Use a WKWebView instead and implement the WKNavigationDelegate method like optional func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) and then read the current URL. Define your action based on that.

You can use this to close the view controller that houses your web view. You will unfortunately have to build your own navigation buttons (the ones the SFSafariViewController has), if you want to allow the user access to those. You don't have to provide them.

like image 2
erik_m_martens Avatar answered Oct 13 '22 19:10

erik_m_martens