ios 如何从 Webview (swift) 打开 Safari View Controller
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38203960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to open Safari View Controller from a Webview (swift)
提问by beginnercoder
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)
}
回答by Bhumit Mehta
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: URL(string: "www.google.com")!)
present(safariVC, animated: true, completion: nil)
safariVC.delegate = self
}
func safariViewControllerDidFinish(controller: SFSafariViewController) {
controller.dismissViewControllerAnimated(true, completion: nil)
}
回答by vikzilla
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)
}
回答by onemillion
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
回答by Anup Gupta
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
使用您的视图控制器导入 SFSafariViewControllerDelegate
class ViewController: UIViewController,SFSafariViewControllerDelegate {...}
Step 3:
第 3 步:
Create A function to Open Safari View Controller.
创建一个函数来打开 Safari 视图控制器。
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 the function openSafariVC
调用函数 openSafariVC
openSafariVC()
Note:Don't forget To Add Navigation Controller with your View Controller.
注意:不要忘记在您的视图控制器中添加导航控制器。
Now your SafariVC is ready to open your Link within an app without Using UIWebViewOe WKWebView
现在您的 SafariVC 已准备好在应用程序中打开您的链接,而无需使用UIWebViewOe WKWebView
回答by Dipen Patel
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
回答by Azharhussain Shaikh
Swift 4.2
斯威夫特 4.2
This is how you can open-up Safari browser within your application.
这是您在应用程序中打开 Safari 浏览器的方式。
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
通过编写自定义函数,您可以自定义该函数的属性 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.
最后也是最重要的是不要忘记将 的委托SFSafariViewController
与您的视图控制器绑定。你可以通过下面提到的extension
代码来做到这一点。
extension YourViewController: SFSafariViewControllerDelegate
{
func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
controller.dismiss(animated: true, completion: nil)
}
}
Happy coding Thank you :)
快乐编码谢谢:)