Closes SFSafariViewController on a certain url

↘锁芯ラ 提交于 2020-06-14 07:01:49

问题


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


回答1:


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:.




回答2:


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.




回答3:


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)
    }
}


来源:https://stackoverflow.com/questions/47259647/closes-sfsafariviewcontroller-on-a-certain-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!