Unable to Trigger JavaScript Actions in WKWebView for W3Schools "Try it Yourself" Button (iOS/UIKIT)

26 views Asked by At

I'm facing an issue while trying to display the W3Schools website within an iOS Swift app using WKWebView. The website loads successfully, but when I attempt to interact with elements like the "Try it Yourself" button, nothing happens.

I've ensured that JavaScript is enabled, and I've set the can-open-windows-automatically property in the storyboard. Below is the relevant code snippet from my ViewController:

import UIKit
import WebKit

class ViewController: UIViewController,WKUIDelegate, WKNavigationDelegate {

    @IBOutlet weak var webView: WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.setup()
        webView.navigationDelegate = self
        
        webView.load(NSURLRequest(url: NSURL(string: "https://www.w3schools.com/tags/att_a_target.asp")! as URL) as URLRequest)
    }

   func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
        if navigationAction.targetFrame == nil {
            webView.load(navigationAction.request)
        }
        return nil
    }
    
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        print("Page Finished Loading")
    }
}


0

There are 0 answers