我正在嘗試創建一個顯示本地 HTML 檔案的 WebView index.html,這個 HTML 檔案有一個鏈接的 JS 外部檔案form.js,我使用 VSCode 創建了這兩個檔案,并且在使用 Live Server 運行它時一切正常。但是在 XCode 上構建和運行應用程式時,HTML 檔案似乎作業得很好,但我沒有收到警報,也沒有收到 JS 檔案中定義的列印陳述句。這是我正在運行的代碼:
SWIFT代碼:
private func loadRequest() {
let htmlPath = Bundle.main.path(forResource: "index", ofType: "html")
guard let htmlPath = htmlPath else { return }
let url = URL(fileURLWithPath: htmlPath)
let request = URLRequest(url: url)
webView.load(request)
}
HTML 檔案:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form</title>
</head>
<body>
<form>
<div>
<label>Name</label>
<input>
</div>
<div>
<label>Last Name</label>
<input>
</div>
<div>
<label>Date of birth</label>
<input type="date" id="birthday" name="birthday">
</div>
<div>
<button>Button1</button>
<input>
</div>
<div>
<button>Button2</button>
<input>
</div>
</form>
<script src="form.js"></script>
</body>
</html>
JS檔案
alert('test');
console.log("This is workig");
我已經將這兩個檔案匯入到我的 XCode 專案中,該專案位于應用程式的主檔案夾中。所以我不確定我做錯了什么,這是我第一次嘗試構建這樣的東西。請問,誰能幫幫我?我一直在到處尋找解決方案,但我對 HTML/JS 代碼并不十分熟悉。提前致謝!(:

uj5u.com熱心網友回復:
您可以使用 JavaScript 代碼提供 webView,但是,WKWebView 無法識別 JavaScript 的“警報”功能。這是一個可能的解決方法:
1-將腳本注入webView
2- 使用 webView 的 runJavaScriptAlertPanelWithMessage 方法接收警報訊息
3-顯示警報的訊息
這是我用來測驗 index.html 和 form.js 檔案的代碼:

import UIKit
import WebKit
class ViewController: UIViewController {
var webView: WKWebView!
var bundle: Bundle {
return Bundle.main
}
var scriptString: String {
if let path = bundle.path(forResource: "form", ofType: "js") {
do {
return try String(contentsOfFile: path)
} catch {
return ""
}
} else {
return ""
}
}
override func viewDidLoad() {
super.viewDidLoad()
let config = WKWebViewConfiguration()
let js = scriptString
let script = WKUserScript(source: js, injectionTime: .atDocumentEnd, forMainFrameOnly: false)
config.userContentController.addUserScript(script)
config.userContentController.add(self, name: "alert")
webView = WKWebView(frame: view.bounds, configuration: config)
webView.uiDelegate = self
webView.navigationDelegate = self
view.addSubview(webView)
NSLayoutConstraint.activate([
webView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
webView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
webView.topAnchor.constraint(equalTo: view.topAnchor),
webView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
loadRequest()
}
private func loadRequest() {
guard let url = bundle.url(forResource: "index", withExtension: "html") else { return }
webView.loadFileURL(url, allowingReadAccessTo: url)
}
}
extension ViewController: WKScriptMessageHandler, WKUIDelegate, WKNavigationDelegate {
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
}
func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo) async {
let alert = UIAlertController(title: "Alert", message: message, preferredStyle: .alert)
let action = UIAlertAction(title: "Ok", style: .default) { ac in
// Do something
}
alert.addAction(action)
present(alert, animated: true, completion: nil)
}
}
輸出:

更多資訊:
WKUserScript 不作業
iOS WKWebView 不顯示 javascript alert() 對話框
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/485617.html
標籤:javascript IOS 迅速 代码 网页浏览
上一篇:如何在SwiftUI視圖之間共享/系結@StateObject?
下一篇:顫振飛鏢排序方法不起作用
