15

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)
  }
}
Kerberos
  • 4,036
  • 3
  • 36
  • 55
lusito92
  • 161
  • 1
  • 2
  • 10

6 Answers6

21

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
Martin M
  • 231
  • 2
  • 5
17

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)
    }
}
Andrew Bogaevskyi
  • 2,251
  • 21
  • 25
  • how do you implement this? I get so many errors If I do it in the view controller class – lusito92 Oct 31 '17 at 15:49
  • @lusito92 I've updated my answer. It works well for me (Xcode 9.0.1, Swift 4). What errors do you have? – Andrew Bogaevskyi Nov 01 '17 at 09:28
  • I updated but still not working javascript look on the top the update please let me know really appreciate – lusito92 Nov 02 '17 at 10:49
  • @lusito92 I've double checked it with https://www.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 http://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. – Andrew Bogaevskyi Nov 06 '17 at 08:31
  • 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..! – Abirami Bala Dec 25 '18 at 13:57
9

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)
  }
}
Md Imran Choudhury
  • 9,343
  • 4
  • 62
  • 60
3

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

Md Imran Choudhury
  • 9,343
  • 4
  • 62
  • 60
  • 4
    'javaScriptEnabled' was deprecated in iOS 14.0: Use WKWebPagePreferences.allowsContentJavaScript to disable content JavaScript on a per-navigation basis – kasoft Feb 18 '21 at 17:47
2

IOS 15.5

add this

webView.configuration.defaultWebpagePreferences.allowsContentJavaScript = true
-1

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
}