我知道这个问题已经被问了很多,我想我已经查看过每一篇关于这个问题的帖子,但我仍然无法让它发挥作用。我是swift的新手,我认为这会阻止我从其他答案中调整代码片段。
所以这是我的问题:
我使用WKWebView在我的应用程序中查看网站。当我点击打开新标签的链接时,没有任何反应。我希望在Safari中打开新标签,或者至少在新的wkwebview中打开。我尝试过这样的答案:https://stackoverflow.com/a/27391215和Open a WKWebview target="_blank" link in Safari以及许多其他类似的答案,但我没有取得任何进展。我需要做什么才能在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
的链接。
我已经突出显示了正确打开链接所必须具备的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
}
}
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)) }
}