'iOS WKWebView Swift javascript enable

I'm trying to develop a simple app to browse my website. However my website contains some javascript and it doesn't successfully show my website.

In the past develop with android the same app and had to activate like this:

webSettings.setJavaScriptEnabled(true);

This currently my code I just have missing the option to enable javascript if somebody can help will really appreciate

import WebKit

class ViewController: UIViewController {

  @IBOutlet weak var webView: WKWebView!

  override func viewDidLoad() {
    super.viewDidLoad()
    let url = URL(string: "http://132.148.136.31:8082")
    let urlRequest = URLRequest(url: url!)

    webView.load(urlRequest)
  }
}


Solution 1:[1]

Working with WKWebView is better to create it from code. And you can set javaScriptEnabled to configuration:

let preferences = WKPreferences()
preferences.javaScriptEnabled = true
let configuration = WKWebViewConfiguration()
configuration.preferences = preferences
let webview = WKWebView(frame: .zero, configuration: configuration)

Update. This is WKWebView in view controller class:

import UIKit
import WebKit

class ViewController: UIViewController {

    private var webView: WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let preferences = WKPreferences()
        preferences.javaScriptEnabled = true
        let configuration = WKWebViewConfiguration()
        configuration.preferences = preferences
        webView = WKWebView(frame: view.bounds, configuration: configuration)
        view.addSubview(webView)
    }
}

Solution 2:[2]

For those who are building for iOS 14 with Swift 5 and Xcode 12, it has changed to the following ...

webView.configuration.defaultWebpagePreferences.allowsContentJavaScript = true

Solution 3:[3]

You are using WKWebView storyboard view so you can directly access the configuration. So use preferences under your WKWebView configuration.

import WebKit

class ViewController: UIViewController {

  @IBOutlet weak var webView: WKWebView!

  override func viewDidLoad() {
    super.viewDidLoad()
    let url = URL(string: "http://132.148.136.31:8082")
    let urlRequest = URLRequest(url: url!)

    // enable JS
    webView.configuration.preferences.javaScriptEnabled = true
    webView.load(urlRequest)
  }
}

Solution 4:[4]

The above answer is right you need to set this value as true to enable JavaScript.

webView.configuration.preferences.javaScriptEnabled = true

But if the JS scripts are from a non-HTTPS source then you need to allow "App Transport Security's Arbitrary Loads".

Here is how to fix that:

  1. Navigate to Project Settings/Target/[your app's target]/Info.
  2. Check whether there is a dictionary called App Transport Security Settings. If not, create it.
  3. Inside it, create a boolean value called Allow Arbitrary Loads (if it's not there yet) and set it to YES.

enter image description here

Solution 5:[5]

try this code :

 override func viewDidLoad() {
    super.viewDidLoad()
    configureWebView()
    currentWebView.load(requestURL)
}

private func configureWebView() {
    webView.navigationDelegate = self
    webView.uiDelegate = self
    webView.allowsBackForwardNavigationGestures = true
    /// javaScript ?? ??
    if #available(iOS 14, *) {
        let preferences = WKWebpagePreferences()
        preferences.allowsContentJavaScript = true
        webView.configuration.defaultWebpagePreferences = preferences
    }
    else {
        webView.configuration.preferences.javaScriptEnabled = true
        /// user interaction?? ??? ?? ? ? ??? ??? ???. iOS??? ???? false??.
        webView.configuration.preferences.javaScriptCanOpenWindowsAutomatically = true
    }
}

func createNewWebView(_ config: WKWebViewConfiguration) -> WKWebView {
    /// autoLayout ?? ?? webView? frame? ?? ??.
    let newWebView = WKWebView(frame: webView.frame,
                               configuration: config)
    newWebView.navigationDelegate = self
    newWebView.uiDelegate = self
    newWebView.allowsBackForwardNavigationGestures = true
    view.addSubview(newWebView)
    popupWebViews.append(newWebView)
    return newWebView
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Martin M
Solution 3 Md Imran Choudhury
Solution 4 Md Imran Choudhury
Solution 5 Taj Noah Alagawani