Hello everyone! Please tell me what the problem might be.
Through WKWebView I go to https://ya.ru/ , I do a search there, for example, “Wikipedia”, after which I try to open the first link, but nothing happens.
import UIKit
import WebKit
class WebViewController: UIViewController {
@IBOutlet weak var webViewOutlet: WKWebView!
override func viewDidLoad () {
super.viewDidLoad ()
self.webViewOutlet.load (URLRequest (url: URL (string: "https://ya.ru/")!))
self.webViewOutlet.allowsBackForwardNavigationGestures = true
}
}
Answer 1, authority 100%
You need to add a protocol, specify its delegate and add a method.
I have commented out the places where I added all this to your code.
import UIKit
import WebKit
class WebViewController: UIViewController, WKUIDelegate {// WKUIDelegate protocol here
@IBOutlet weak var webViewOutlet: WKWebView!
override func viewDidLoad () {
super.viewDidLoad ()
self.webViewOutlet? .uiDelegate = self // Delegate
self.webViewOutlet.load (URLRequest (url: URL (string: "https://ya.ru/")!))
self.webViewOutlet.allowsBackForwardNavigationGestures = true
}
// And this method
func webView (_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) - & gt; WKWebView? {
if navigationAction.targetFrame == nil {
webView.load (navigationAction.request)
}
return nil
}
}