[翻译]  How to open Safari View Controller from a Webview (swift)

[CHINESE]  如何从Webview(swift)打开Safari View Controller


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?

我有一个当前使用webview的应用程序,当在webview中单击某些链接时,它会在Safari中打开这些链接。我现在想要实现Safari视图控制器(SVC),而不是将其启动到Safari应用程序。我做了研究,看了SVC的例子;但是,我所看到的只是通过点击按钮打开SVC。有没有人有任何建议让我看看或尝试?

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

5 个解决方案

#1


12  

If I am understanding correctly you are loading a page on webview which has certain links now when user clicks on link you want to open those page in SVC. You can detect link click in webview using following delegate method and then open SVC from there.

如果我正确理解您正在加载webview上的页面,当用户点击链接时您现在可以在SVC中打开这些页面时具有某些链接。您可以使用以下委托方法检测webview中的链接点击,然后从那里打开SVC。

EDIT

编辑

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

基于编辑过的问题我可以看到你没有调用showLinksClicked func,你可以调用这个函数,因为我已经在下面的代码中更新了它应该可以工作。

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

#2


9  

For Swift 3:

对于Swift 3:

First, import SafariServices and integrate the delegate into your class:

首先,导入SafariServices并将委托集成到您的类中:

import SafariServices

class YourViewController: SFSafariViewControllerDelegate {

Then, to open Safari with the specified url:

然后,使用指定的URL打开Safari:

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 dismiss safari when the user is finished:

现在,您可以实现委托回调以在用户完成时解除safari:

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

#3


6  

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 as well:

您可能还需要将其添加到类的顶部:

import SafariServices

#4


2  

Follow the steps :

按照步骤 :

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

在您的控制器文件(例如ViewController.swift)上导入SafarriServices。

import SafariServices

Then where you want to open link write

然后你想在哪里打开链接写

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

#5


0  

Solution For Swift 4

Swift 4的解决方案

Step 1:

步骤1:

import Safari Service In you Class

导入Safari服务在你的类

import SafariServices

Step 2:

第2步:

Import SFSafariViewControllerDelegate in With your View Controller

使用View Controller导入SFSafariViewControllerDelegate

class ViewController: UIViewController,SFSafariViewControllerDelegate {...}

Step 3:

第3步:

Create A function to Open Safari View Controller.

创建一个打开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:

步骤4:

call the function openSafariVC

调用函数openSafariVC

openSafariVC()

Note: Don't forget To Add Navigation Controller with your View Controller.

注意:不要忘记使用View Controller添加导航控制器。

Now your SafariVC is ready to open your Link within an app without Using UIWebView Oe WKWebView

现在,您的SafariVC已准备好在应用程序中打开您的链接,而无需使用UIWebView Oe WKWebView


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2018 ITdaan.com 粤ICP备14056181号