WKWebView target="_blank" link 在 safari 中打开新标签页 ios11, swift 4
WKWebView target="_blank" link open new tab in safari ios11, swift 4
我知道这个问题已经被问了很多,我想我已经查看了关于这个的每一个 post 但我仍然无法让它工作。我是 swift 的新手,我认为这阻碍了我从其他答案中改编代码片段。
所以这是我的问题:
我正在使用 WKWebView 在我的应用程序中查看网站。当我单击打开新选项卡的 link 时,没有任何反应。我希望新选项卡在 safari 中打开,或者至少在新的 wkwebview 中打开。我已经尝试从 , and 和许多其他类似的答案中实现这个答案,但没有取得任何进展。我需要做什么才能在 swift 4 中完成这项工作?
目前我只有这个,因为我无法实施我成功找到的任何其他解决方案:
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
if navigationAction.targetFrame == nil {
webView.load(navigationAction.request)
}
return nil
}
但是好像没什么作用。如果有人能帮助我指出正确的方向,我将不胜感激。
我粘贴了 WKWebView 项目(从文件夹加载本地 html)的一些示例代码,该项目需要具有 target=_blank
的链接才能在新浏览器 window.
我已经强调了获得正确打开链接所必须具备的 3 件事。
class ViewController extends WKUIDelegate
self.webView.uiDelegate = self
- 使用
UIApplication.shared.open
代替webView.load
让我知道它有效,如果有人可以对下面的示例代码提出改进建议,那对我也会有帮助:)
下面 Xcode 9.2、Swift 4 的完整示例代码。
祝你好运
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {
@IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
self.webView.uiDelegate = self
let htmlPath = Bundle.main.path(forResource: "index", ofType: "html", inDirectory: "www")
let htmlUrl = URL(fileURLWithPath: htmlPath!)
let htmlDir = Bundle.main.url(forResource: "www", withExtension: nil)
webView.loadFileURL(htmlUrl, allowingReadAccessTo: htmlDir!)
}
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
if navigationAction.targetFrame == nil {
//webView.load(navigationAction.request)
UIApplication.shared.open(navigationAction.request.url!, options: [:])
}
return nil
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override var prefersStatusBarHidden: Bool {
return true
}
}
详情
- Xcode10.2 (10E125),Swift5
解决方案
extension ViewController: WKUIDelegate {
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
// push new screen to the navigation controller when need to open url in another "tab"
if let url = navigationAction.request.url, navigationAction.targetFrame == nil {
let viewController = ViewController()
DispatchQueue.main.async { [weak self] in
self?.navigationController?.pushViewController(viewController, animated: true)
}
// .....
return viewController.webView
}
return nil
}
完整样本
Info.plist
添加您的 Info.plist 传输安全设置
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
代码
import UIKit
import WebKit
class ViewController: UIViewController {
private lazy var url = URL(string: "https://google.com")!
private weak var webView: WKWebView?
func initWebView(configuration: WKWebViewConfiguration) {
if webView != nil { return }
let webView = WKWebView(frame: UIScreen.main.bounds, configuration: configuration)
webView.uiDelegate = self
view.addSubview(webView)
self.webView = webView
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if webView == nil { initWebView(configuration: WKWebViewConfiguration()) }
webView?.load(url: url)
}
}
extension ViewController: WKUIDelegate {
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
// push new screen to the navigation controller when need to open url in another "tab"
if let url = navigationAction.request.url, navigationAction.targetFrame == nil {
let viewController = ViewController()
viewController.initWebView(configuration: configuration)
viewController.url = url
DispatchQueue.main.async { [weak self] in
self?.navigationController?.pushViewController(viewController, animated: true)
}
return viewController.webView
}
return nil
}
}
extension WKWebView {
func load(url: URL) { load(URLRequest(url: url)) }
}
我知道这个问题已经被问了很多,我想我已经查看了关于这个的每一个 post 但我仍然无法让它工作。我是 swift 的新手,我认为这阻碍了我从其他答案中改编代码片段。
所以这是我的问题:
我正在使用 WKWebView 在我的应用程序中查看网站。当我单击打开新选项卡的 link 时,没有任何反应。我希望新选项卡在 safari 中打开,或者至少在新的 wkwebview 中打开。我已经尝试从 , and
目前我只有这个,因为我无法实施我成功找到的任何其他解决方案:
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
if navigationAction.targetFrame == nil {
webView.load(navigationAction.request)
}
return nil
}
但是好像没什么作用。如果有人能帮助我指出正确的方向,我将不胜感激。
我粘贴了 WKWebView 项目(从文件夹加载本地 html)的一些示例代码,该项目需要具有 target=_blank
的链接才能在新浏览器 window.
我已经强调了获得正确打开链接所必须具备的 3 件事。
class ViewController extends WKUIDelegate
self.webView.uiDelegate = self
- 使用
UIApplication.shared.open
代替webView.load
让我知道它有效,如果有人可以对下面的示例代码提出改进建议,那对我也会有帮助:)
下面 Xcode 9.2、Swift 4 的完整示例代码。
祝你好运
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {
@IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
self.webView.uiDelegate = self
let htmlPath = Bundle.main.path(forResource: "index", ofType: "html", inDirectory: "www")
let htmlUrl = URL(fileURLWithPath: htmlPath!)
let htmlDir = Bundle.main.url(forResource: "www", withExtension: nil)
webView.loadFileURL(htmlUrl, allowingReadAccessTo: htmlDir!)
}
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
if navigationAction.targetFrame == nil {
//webView.load(navigationAction.request)
UIApplication.shared.open(navigationAction.request.url!, options: [:])
}
return nil
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override var prefersStatusBarHidden: Bool {
return true
}
}
详情
- Xcode10.2 (10E125),Swift5
解决方案
extension ViewController: WKUIDelegate {
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
// push new screen to the navigation controller when need to open url in another "tab"
if let url = navigationAction.request.url, navigationAction.targetFrame == nil {
let viewController = ViewController()
DispatchQueue.main.async { [weak self] in
self?.navigationController?.pushViewController(viewController, animated: true)
}
// .....
return viewController.webView
}
return nil
}
完整样本
Info.plist
添加您的 Info.plist 传输安全设置
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
代码
import UIKit
import WebKit
class ViewController: UIViewController {
private lazy var url = URL(string: "https://google.com")!
private weak var webView: WKWebView?
func initWebView(configuration: WKWebViewConfiguration) {
if webView != nil { return }
let webView = WKWebView(frame: UIScreen.main.bounds, configuration: configuration)
webView.uiDelegate = self
view.addSubview(webView)
self.webView = webView
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if webView == nil { initWebView(configuration: WKWebViewConfiguration()) }
webView?.load(url: url)
}
}
extension ViewController: WKUIDelegate {
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
// push new screen to the navigation controller when need to open url in another "tab"
if let url = navigationAction.request.url, navigationAction.targetFrame == nil {
let viewController = ViewController()
viewController.initWebView(configuration: configuration)
viewController.url = url
DispatchQueue.main.async { [weak self] in
self?.navigationController?.pushViewController(viewController, animated: true)
}
return viewController.webView
}
return nil
}
}
extension WKWebView {
func load(url: URL) { load(URLRequest(url: url)) }
}