如何从 Webview 打开 Safari View Controller (swift)
How to open Safari View Controller from a Webview (swift)
我有一个当前正在使用 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)
}
这段代码将允许您执行此操作。
let safariVC = SFSafariViewController(URL: NSURL(string: "https://www.google.co.uk")!)
self.presentViewController(safariVC, animated: true, completion: nil)
safariVC.delegate = self
您可能还需要将此添加到 class 的顶部:
import SafariServices
如果我理解正确,您正在 webview 上加载一个页面,当用户点击 link 时,您现在想在 SVC 中打开这些页面。您可以使用以下委托方法检测 link 在 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)
}
按照以下步骤操作:
在您的控制器文件中(例如 ViewController.swift)导入 SafarriServices。
import SafariServices
然后在你要打开的地方写link
let controller = SFSafariViewController(URL: NSURL(string: "https://www.google.co.uk")!)
self.presentViewController(controller, animated: true, completion: nil)
controller = self
对于Swift 3:
首先,导入 SafariServices 并将委托集成到您的 class:
import SafariServices
class YourViewController: SFSafariViewControllerDelegate {
然后,用指定的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
现在您可以实现委托回调以在用户完成后关闭 Safari:
func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
controller.dismiss(animated: true, completion: nil)
}
Solution For Swift 4
第 1 步:
在你里面导入 Safari 服务 Class
import SafariServices
第 2 步:
使用您的视图控制器导入 SFSafariViewControllerDelegate
class ViewController: UIViewController,SFSafariViewControllerDelegate {...}
第 3 步:
创建一个函数来打开 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)
}
第 4 步:
调用函数 openSafariVC
openSafariVC()
Note: Don't forget To Add Navigation Controller with your View
Controller.
现在您的 SafariVC 已准备好在应用程序中打开您的 Link 而无需使用 UIWebView Oe WKWebView
Swift 4.2
这是您在应用程序中打开 Safari 浏览器的方式。
import SafariServices
任何时候你想打开,就像在
上一样
@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")
}
通过编写自定义函数就可以自定义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
}
最后也是最重要的是不要忘记将 SFSafariViewController
的委托与您的视图控制器绑定。你可以通过下面提到的 extension
代码来做到这一点。
extension YourViewController: SFSafariViewControllerDelegate
{
func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
controller.dismiss(animated: true, completion: nil)
}
}
编码愉快谢谢:)
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)
}
}
我有一个当前正在使用 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)
}
这段代码将允许您执行此操作。
let safariVC = SFSafariViewController(URL: NSURL(string: "https://www.google.co.uk")!)
self.presentViewController(safariVC, animated: true, completion: nil)
safariVC.delegate = self
您可能还需要将此添加到 class 的顶部:
import SafariServices
如果我理解正确,您正在 webview 上加载一个页面,当用户点击 link 时,您现在想在 SVC 中打开这些页面。您可以使用以下委托方法检测 link 在 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)
}
按照以下步骤操作:
在您的控制器文件中(例如 ViewController.swift)导入 SafarriServices。
import SafariServices
然后在你要打开的地方写link
let controller = SFSafariViewController(URL: NSURL(string: "https://www.google.co.uk")!)
self.presentViewController(controller, animated: true, completion: nil)
controller = self
对于Swift 3:
首先,导入 SafariServices 并将委托集成到您的 class:
import SafariServices
class YourViewController: SFSafariViewControllerDelegate {
然后,用指定的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
现在您可以实现委托回调以在用户完成后关闭 Safari:
func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
controller.dismiss(animated: true, completion: nil)
}
Solution For Swift 4
第 1 步:
在你里面导入 Safari 服务 Class
import SafariServices
第 2 步:
使用您的视图控制器导入 SFSafariViewControllerDelegate
class ViewController: UIViewController,SFSafariViewControllerDelegate {...}
第 3 步:
创建一个函数来打开 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)
}
第 4 步:
调用函数 openSafariVC
openSafariVC()
Note: Don't forget To Add Navigation Controller with your View Controller.
现在您的 SafariVC 已准备好在应用程序中打开您的 Link 而无需使用 UIWebView Oe WKWebView
Swift 4.2
这是您在应用程序中打开 Safari 浏览器的方式。
import SafariServices
任何时候你想打开,就像在
上一样@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")
}
通过编写自定义函数就可以自定义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
}
最后也是最重要的是不要忘记将 SFSafariViewController
的委托与您的视图控制器绑定。你可以通过下面提到的 extension
代码来做到这一点。
extension YourViewController: SFSafariViewControllerDelegate
{
func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
controller.dismiss(animated: true, completion: nil)
}
}
编码愉快谢谢:)
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)
}
}