我一直在尝试使用 UIwebview 动态加载 JS 文件/库(交易视图)。我已成功将 JS 文件放入 .html 文件中,但在 webview 加载 .html 文件之前我无法传递变量。我的一些代码片段:
<!DOCTYPE html>
<html>
<head>
<title>TradingView Charting Library demo </title>
<!-- Fix for iOS Safari zooming bug -->
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<script type="text/javascript" src="charting_library/charting_library.min.js"></script>
<script type="text/javascript" src="datafeeds/udf/dist/polyfills.js"></script>
<script type="text/javascript" src="datafeeds/udf/dist/bundle.js"></script>
<script type="text/javascript" src="interface_js.js" one='BTC_EUR'></script>
<script>
var widget;
function setSymbol(symbol) {
console.log(symbol)
widget.chart().setSymbol("ADA_BTC");
}
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
TradingView.onready(function() {
widget = new TradingView.widget({
fullscreen: true,
symbol: 'BTC_EUR',
interval: 'D',
container_id: "tv_chart_container",
// BEWARE: no trailing slash is expected in feed URL
datafeed: new Datafeeds.UDFCompatibleDatafeed("http://example.com"),
library_path: "charting_library/",
locale: getParameterByName('lang') || "en",
disabled_features: ["use_localstorage_for_settings"],
preset: "mobile"
});
})
</script>
</head>
<body style="margin:0px;">
<div id="tv_chart_container"></div>
</body>
快速片段:
if let url = Bundle.main.url(forResource: "mobile_white1", withExtension:"html") {
let request = NSURLRequest(url: url)
self.webView.loadRequest(request as URLRequest)
self.webView.stringByEvaluatingJavaScript(from: "setSymbol(\(selectedPair.uppercased()))")
}
else { self.html = "<html><head><title>TradingView Charting Library demo</title><meta name='viewport' content='width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0'><meta http-equiv='X-UA-Compatible' content='IE=Edge'></head><body style='margin:0px;'><script type='text/javascript' src='charting_library/charting_library.min.js'></script><script type='text/javascript' src='datafeeds/udf/dist/polyfills.js'></script><script type='text/javascript' src='datafeeds/udf/dist/bundle.js'></script><script type='text/javascript' src='interface_js.js' one='BTC_EUR'></script><div id='tv_chart_container'></div></body></html>"
webView.loadHTMLString(self.html, baseURL: nil)
}
拦截.js 片段:
var widget;
var selectedSymbol = document.currentScript.getAttribute('one');
TradingView.onready(() => {
widget = new TradingView.widget({
fullscreen: true,
symbol: selectedSymbol,
interval: 'D',
debug: true,
container_id: "tv_chart_container",
datafeed: new Datafeeds.UDFCompatibleDatafeed("http://example.com"),
library_path: "charting_library/",
locale: "en",
disabled_features: ["use_localstorage_for_settings"],
overrides: {
'mainSeriesProperties.style': 3
}
});
})
如果我mobile_white
输入forResource(swift)
,“if”条件会执行,一切正常,但不能动态传递 html 文件中的任何内容。
然后我尝试将 html 字符串放入 loadHTMLString 和 baseURL(nil) 中,它现在不加载 JS 库。只有内联 JS 有效。我已经通过了很多 stackoverflow 解决方案并用谷歌搜索了很多东西,但根本无法加载 JS 文件。我也将 baseURL 设置为 @"http" ,但没有成功。
我通过调试发现,当我使用 loadRequest 时,swift 使用“file:///”,但是当我使用“webView.loadHTMLString”时它切换到“applewebdata://”,而这个“applewebdata”的东西并没有让 webview加载 JS 文件。
PS:使用 swift 代码中可用的所选符号(即 ETH/USD)加载 TradingView。
谢谢。