【发布时间】:2021-08-27 00:13:27
【问题描述】:
我有一个应用程序,它有一个 webview 选项卡。在我使用的特定网站上,它不会打开链接。在 safari 或其他浏览器上时,所有链接都会自动打开一个新选项卡。我想让用户保持在同一个 webview 中。我怎样才能做到这一点?下面是我的代码。我可以使用 React Native 和 Flutter 做到这一点,但无法迅速解决。请帮忙。
class ExampleSecondViewController: UIViewController, WKUIDelegate {
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string:"https://travelsecrets.live")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
webView.allowsBackForwardNavigationGestures = true
webView.addObserver(self, forKeyPath: #keyPath(WKWebView.estimatedProgress),
options: .new, context: nil)
webView.configuration.preferences.javaScriptEnabled = true
}
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change:
[NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "estimatedProgress" {
print(Float(webView.estimatedProgress))
}
}
}
【问题讨论】: