gpt4 book ai didi

ios - 如何从 Webview 打开 Safari View Controller (swift)

转载 作者:IT王子 更新时间:2023-10-29 05:28:23 25 4
gpt4 key购买 nike

我有一个当前正在使用 webview 的应用程序,当在 webview 中单击某些链接时,它会在 Safari 中打开这些链接。我现在想实现 Safari View Controller(SVC) 而不是将其引导至 Safari 应用程序。我做了研究并查看了 SVC 上的示例;但是,我所看到的只是通过单击按钮打开 SVC 的那些。有没有人有任何建议供我查看或尝试?

这是我的一些代码:

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

最佳答案

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

编辑

根据编辑后的问题,我可以看到您没有调用 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: URL(string: "www.google.com")!)
present(safariVC, animated: true, completion: nil)
safariVC.delegate = self
}

func safariViewControllerDidFinish(controller: SFSafariViewController) {
controller.dismissViewControllerAnimated(true, completion: nil)
}

关于ios - 如何从 Webview 打开 Safari View Controller (swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38203960/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com