0

I have an app that has a tab that is a webview. On the specific site that I use it will not open links. All links automatically open a new tab when on safari or another browser. I want to keep the user in the same webview. How can I do this? Below is my code. I can do this with React Native and Flutter but can't figure it out in swift. Please help.

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))
     }
  }
  }
DCnative
  • 11
  • 3

2 Answers2

1

This is a solution for web view page open links within and outside of the app

 class ViewController: WKNavigationDelegate {   
    

 @IBOutlet weak var web_view: WKWebView!

override func viewDidLoad() {
   super.viewDidLoad()

   web_view.navigationDelegate = self

// This is for normal URL loading...

   let myURL = URL(string:"https://travelsecrets.live")
   let myRequest = URLRequest(url: myURL!)
   web_view.load(myRequest)

// This is for HTML loading...

   let htmlString = "<html><body><p> Hello Buddies! This is a content area..!</p></body></html>";
   web_view.loadHTMLString(htmlString, baseURL: nil)

}

Delegate Method for Openlink Navigation functionality Outside of the Application

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
   guard case .linkActivated = navigationAction.navigationType,
                  let url = navigationAction.request.url
            else {
              decisionHandler(.allow)
              return
            }
            decisionHandler(.cancel)
        UIApplication.shared.open(url)
       }

Openlink Navigation functionality Within the Application no need to add the delegate method. I think it will work automatically.

The above solution is working for me...!

My Xcode version is 14.0, The Swift version is 5+.

Vishnu
  • 348
  • 2
  • 7
-1

You need to implement navigationDelegate for your webView:

override func loadView() {
    let webConfiguration = WKWebViewConfiguration()
    webView = WKWebView(frame: .zero, configuration: webConfiguration)
    webView.uiDelegate = self
    // this delegate
    webView.navigationDelegate = self
    view = webView
}

And use this method:

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
 if navigationAction.navigationType == WKNavigationType.linkActivated {
    webView.load(navigationAction.request)
    decisionHandler(.cancel)
    return
 }
 decisionHandler(.allow)
}

Based on this thread: https://stackoverflow.com/a/51660446/10492380

rickramirr
  • 19
  • 3