iOS WKWebView Swift JavaScript启用

17

我正在尝试开发一个简单的应用程序来浏览我的网站。然而,我的网站包含一些JavaScript代码,它无法成功显示我的网站。

以前使用Android开发同样的应用程序,必须像这样激活:

webSettings.setJavaScriptEnabled(true);

这是我目前的代码,我只是缺少启用JavaScript的选项,如果有人能帮助我,我将非常感激。

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)
  }
}
6个回答

24

对于那些使用Swift 5和Xcode 12构建iOS 14的开发者,它已经变成了以下内容...

webView.configuration.defaultWebpagePreferences.allowsContentJavaScript = true

这对我没有用,它仍然显示我的浏览器(Webview)中禁用了JS。 - Siempay

17

使用WKWebView最好是从代码中创建。您可以将javaScriptEnabled设置为configuration

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

更新。这是视图控制器类中的WKWebView:

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)
    }
}

你怎么实现这个功能?如果我在视图控制器类中实现它,会出现很多错误。 - lusito92
@lusito92 我已经更新了我的答案。它在我这里运行良好(Xcode 9.0.1,Swift 4)。你遇到了什么错误? - Andrew Bogaevskyi
我已经更新了,但JavaScript仍然无法正常工作,请查看顶部的更新并告诉我,非常感谢。 - lusito92
@lusito92 我已经通过 https://www.google.com 进行了双重检查。它可以工作!使用 .javaScriptEnabled = true,我可以打开侧边栏,如果是 false - 我就不能。您确定 JavaScript 在您的页面上按 URl http://132.148.136.31:8082 工作吗?您是否在 Safari 中尝试过?您还可以在 Mac 上使用 Safari 连接设备调试 Webview。 - Andrew Bogaevskyi
它对我不起作用。在Web视图中,有一个函数,当单击按钮/文本字段时显示自动填充下拉菜单。启用JavaScript后,在Android上可以正常工作。但是,即使按照上面的代码启用了JavaScript,它对我也不起作用。有什么解决方案吗?将不胜感激。谢谢..! - Abirami Bala

9
您正在使用WKWebView故事板视图,因此您可以直接访问配置。因此,请在您的WKWebView配置下使用首选项。
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)
  }
}

1
更加简洁的语法! - Heitor
@Md Imran Choudhury,我该如何检查我的JS是否成功加载。 - Ruchira More
您可以设置任何JS操作来检查您的网站。 - Md Imran Choudhury
是的,我已经编写了JS函数来在我的Swift类中显示和评估它,它可以正常工作。但在我的情况下,JavaScript没有被调用,这是因为我们在HTML中设置的脚本路径吗?还是因为JS接口没有被调用? - Ruchira More
1
@MdImranChoudhury 当我将我的脚本注入到我的WKWebview中,然后才能在加载HTML后显示“JS Alert”。问题已解决! - Ruchira More

3

以上答案是正确的,您需要将该值设置为true才能启用JavaScript。

webView.configuration.preferences.javaScriptEnabled = true

但如果JS脚本来自非HTTPS源,则需要允许"应用程序传输安全性的任意加载"

以下是解决方法:

  1. 导航到项目设置/目标/[您的应用程序的目标]/信息。
  2. 检查是否有名为"应用程序传输安全设置"的字典。如果没有,请创建它。
  3. 在其中创建一个名为"允许任意加载"(如果还没有)的布尔值,并将其设置为YES。

enter image description here


4
“javaScriptEnabled”在iOS 14.0中已经被弃用:使用“WKWebPagePreferences.allowsContentJavaScript”来针对每个导航禁用内容JavaScript。 - kasoft

2

iOS 15.5

添加此内容

webView.configuration.defaultWebpagePreferences.allowsContentJavaScript = true

-1

尝试这段代码:

 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
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接