How to open Safari View Controller from Webview (fast)

I have an application that currently uses web browsing, and when certain links are displayed in a web browser, it opens those links in Safari. Now I want to implement Safari View Controller (SVC) instead of loading it into a Safari application. I did research and looked at examples on SVC; however, all that I see are those that open SVC with the click of a button. Anyone have suggestions for me to watch or try?

Here is my code:

func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool { if navigationType == UIWebViewNavigationType.LinkClicked { let host = request.URL!.host!; if (host != "www.example.com"){ return true } else { UIApplication.sharedApplication().openURL(request.URL!) return false } return true } func showLinksClicked() { let safariVC = SFSafariViewController(URL: NSURL(string: "www.example.com")!) self.presentViewController(safariVC, animated: true, completion: nil) safariVC.delegate = self } func safariViewControllerDidFinish(controller: SFSafariViewController) { controller.dismissViewControllerAnimated(true, completion: nil) } 
+11
source share
6 answers

If I understand correctly, you are loading a page in a webview that has certain links when the user clicks the link that you want to open in SVC. You can detect the link in a web browser using the following delegation method, and then open SVC.

EDIT

Based on the edited question, I see that you are not calling the showLinksClicked function, you can call this function since I updated the following code and it should work.

 func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool { if navigationType == UIWebViewNavigationType.LinkClicked { self.showLinksClicked() return false } return true; } func showLinksClicked() { let safariVC = SFSafariViewController(URL: NSURL(string: "www.example.com")!) self.presentViewController(safariVC, animated: true, completion: nil) safariVC.delegate = self } func safariViewControllerDidFinish(controller: SFSafariViewController) { controller.dismissViewControllerAnimated(true, completion: nil) } 
+16
source

For Swift 3:

First import SafariServices and integrate the delegate into your class:

 import SafariServices class YourViewController: SFSafariViewControllerDelegate { 

Then, to open Safari with the specified URL:

 let url = URL(string: "http://www,google.com")! let controller = SFSafariViewController(url: url) self.present(controller, animated: true, completion: nil) controller.delegate = self 

And now you can implement the delegate callback to disable the safari when the user is finished:

 func safariViewControllerDidFinish(_ controller: SFSafariViewController) { controller.dismiss(animated: true, completion: nil) } 
+13
source

This piece of code will allow you to do this.

 let safariVC = SFSafariViewController(URL: NSURL(string: "https://www.google.co.uk")!) self.presentViewController(safariVC, animated: true, completion: nil) safariVC.delegate = self 

You may need to add this to the top of the class:

 import SafariServices 
+8
source

Follow these steps:

In your controller file (e.g. ViewController.swift), import SafarriServices.

 import SafariServices 

Then, where you want to open the link, write

 let controller = SFSafariViewController(URL: NSURL(string: "https://www.google.co.uk")!) self.presentViewController(controller, animated: true, completion: nil) controller = self 
+2
source

Solution For Swift 4

Step 1:

Import a Safari service into your class

 import SafariServices 

Step 2:

Import SFSafariViewControllerDelegate using your View Controller

 class ViewController: UIViewController,SFSafariViewControllerDelegate {...} 

Step 3:

Create a function to open the Safari View Controller.

  func openSafariVC() { let safariVC = SFSafariViewController(url: NSURL(string: "https://www.google.com")! as URL) self.present(safariVC, animated: true, completion: nil) safariVC.delegate = self } func safariViewControllerDidFinish(_ controller: SFSafariViewController) { controller.dismiss(animated: true, completion: nil) } 

Step 4:

call openSafariVC function

 openSafariVC() 

Note: Remember to add a navigation controller with your controller view.

Now your SafariVC is ready to open the link in the application without using the UIWebView Oe WKWebView

+2
source

Swift 4.2

Here's how you can open the Safari browser in your application.

 import SafariServices 

whenever you want to open how wise on

 @IBAction func btnOpenWebTapped(_ sender: UIButton) { self.openWeb(contentLink: "https://www.google.com") } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { self.openWeb(contentLink: "https://www.google.com") } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { self.openWeb(contentLink: "https://www.google.com") } 

write a custom function like you, and you can set the properties of SFSafariViewController, preferredBarTintColor, preferredControlTintColor, dismissButtonStyle, barCollapsingEnabled

 func openWeb(contentLink : String){ let url = URL(string: contentLink)! let controller = SFSafariViewController(url: url) controller.preferredBarTintColor = UIColor.darkGray controller.preferredControlTintColor = UIColor.groupTableViewBackground controller.dismissButtonStyle = .close controller.configuration.barCollapsingEnabled = true self.present(controller, animated: true, completion: nil) controller.delegate = self } 

Last and most important: Remember to associate the SFSafariViewController delegate with your view controller. You can do this with the extension code below.

 extension YourViewController: SFSafariViewControllerDelegate { func safariViewControllerDidFinish(_ controller: SFSafariViewController) { controller.dismiss(animated: true, completion: nil) } } 

Good coding Thank you :)

0
source

All Articles