Home ios WKWebView won't open link

WKWebView won’t open link

Author

Date

Category

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
 }
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions