iOS WKWebView Swift javascript enable

35,434

Solution 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

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

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

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

Share:
35,434
lusito92
Author by

lusito92

Updated on July 21, 2022

Comments

  • lusito92
    lusito92 almost 2 years

    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)
      }
    }
    
  • lusito92
    lusito92 over 6 years
    how do you implement this? I get so many errors If I do it in the view controller class
  • Andrew Bogaevskyi
    Andrew Bogaevskyi over 6 years
    @lusito92 I've updated my answer. It works well for me (Xcode 9.0.1, Swift 4). What errors do you have?
  • lusito92
    lusito92 over 6 years
    I updated but still not working javascript look on the top the update please let me know really appreciate
  • Andrew Bogaevskyi
    Andrew Bogaevskyi over 6 years
    @lusito92 I've double checked it with google.com. It works! with .javaScriptEnabled = true I can open side bar, and if false - I cant. Are you sure that JavaScript works on your page by URl 132.148.136.31:8082 ? Did you try it in Safari? You also could debug you webview with a connected device in Safari on mac.
  • Abirami Bala
    Abirami Bala over 5 years
    It doesn't work for me. there is a function to show an auto fill drop down when clicking a button/ textfield in a webview. and which is working fine on android after enabling the javascript. but it doesn't work for me even if i enable the javascript as per your code above. Any solution..? would be appreciated. Thanks..!
  • Heitor
    Heitor almost 5 years
    Much cleaner syntax!
  • Ruchira More
    Ruchira More over 4 years
    @Md Imran Choudhury how I can check that my JS is loaded successfully.
  • Md Imran Choudhury
    Md Imran Choudhury over 4 years
    You can set any JS action to your website to check.
  • Ruchira More
    Ruchira More over 4 years
    Yes I have written JS function to display and evaluated in my swift class it is working. But in my case javascript is not calling is this because of script path which we are setting in HTML ? Is it because of JS interface not calling?
  • Ruchira More
    Ruchira More over 4 years
    @MdImranChoudhury when I injected my script into my WKWebview then only I am able to display "JS Alert" after loading HTML. Resolved issue!
  • Siempay
    Siempay over 3 years
    this did not work for me, it still shows that js is disabled in my browser (webview)
  • kasoft
    kasoft about 3 years
    'javaScriptEnabled' was deprecated in iOS 14.0: Use WKWebPagePreferences.allowsContentJavaScript to disable content JavaScript on a per-navigation basis