通过以下设置,我可以在模拟器上运行我的应用,并且样式表已正确应用于内容。但是,在设备上运行完全相同的代码会导致样式表被忽略。
我如何设置WKWebView
:
let contentController = WKUserContentController()
let userScript = WKUserScript(source: "my script goes here",
injectionTime: .atDocumentStart,
forMainFrameOnly: false)
contentController.addUserScript(userScript)
let configuration = WKWebViewConfiguration()
configuration.userContentController = contentController
webView = WKWebView(frame: .zero, configuration: configuration)
// Finish setting up & running web view here
对于以上部分,将injectionTime
更改为.atDocumentEnd
,将forMainFrameOnly
更改为true
无效。
还有一个简化的index.html文件,该文件非常空:
<html>
<head>
<title>Style This Page</title>
</head>
<link rel="stylesheet" type="text/css" href="style.css" />
<body onload="runScript()">
<div id="scriptAttachesHere"></div>
<script>
let script
window.onload = function() {
let options = { }
script = new window.SCRIPT.Name(document.getElementById('scriptAttachesHere'), options);
}
</script>
</body>
</html>
以及样式表中的一小段代码:
.script-container .devices .contentContainer::before { height: unset !important; }
所有名称都被掩盖了,所以我可能做错了。
答案 0 :(得分:0)
所以事实证明这很简单。
Apple不赞成将本地文件加载到WKWebView
中,因此我通过将样式表直接合并到HTML代码而不是独立的样式表来解决此问题。
<html>
<head>
<title>Style This Page</title>
</head>
<style>
.script-container .devices .contentContainer::before { height: unset !important; }
</style>
<body onload="runScript()">
<div id="scriptAttachesHere"></div>
<script>
let script
window.onload = function() {
let options = { }
script = new window.SCRIPT.Name(document.getElementById('scriptAttachesHere'), options);
}
</script>
</body>
</html>
请注意删除了用于链接到本地样式表的代码。