Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open Safari View Controller from a Webview (swift)

I have an app that is currently using a webview and when certain links are clicked in the webview, it opens those links in Safari. I now want to implement the Safari View Controller(SVC) instead of booting it to the Safari app. I have done research and looked at examples on the SVC; however, all I see are ones that open the SVC from the click of a button. Does anyone have any suggestions for me to look at or to try?

Here is some of 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)
}
like image 488
beginnercoder Avatar asked Jul 05 '16 12:07

beginnercoder


People also ask

How do I view a controller in Safari?

Opening Content in Safari View Controller Run the app and tap the bottom button, "Open with safari view controller", to see the content now being displayed inside a SFSafariViewController instance. We've now let the user stay inside of our app and they have all the advantages of Safari.

Does Safari use WebView?

A WebView uses webkit engine to render html which is what safari also uses.


2 Answers

import SafariServices

class ViewController: UIViewController, SFSafariViewControllerDelegate {

 override func viewDidLoad() {
     super.viewDidLoad()
     DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
         let url = URL(string: "http://www.google.com")!
         let controller = SFSafariViewController(url: url)
         self.present(controller, animated: true, completion: nil)
         controller.delegate = self
     }
 }

 func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
     dismiss(animated: true)
 }
}
like image 183
Vinoth Anandan Avatar answered Sep 21 '22 12:09

Vinoth Anandan


Swift 4.2

This is how you can open-up Safari browser within your application.

import SafariServices

whenever you want to open, like 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")
}

by write the custom function be like and you can customise the properties of the 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 the most important thing is don't forget to bind the delegate of the SFSafariViewController with your view controller. you can do this by below mentioned extension code.

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

Happy coding Thank you :)

like image 45
Azharhussain Shaikh Avatar answered Sep 18 '22 12:09

Azharhussain Shaikh