在我的项目中,我已经将字符串从 swift 传递到 JavaScript,并在警报中显示它完美地工作。但是如果我传递数组或字典意味着返回未定义的值,例如
[对象对象],[对象对象],[对象对象],[对象对象],[对象对象],[对象对象]
但我不知道如何解决这个问题。
在这里,我附上了 webview 响应的屏幕截图。
在这里,我分享了我尝试过的代码,
override func viewDidLoad() {
super.viewDidLoad()
webView.delegate = self
loadHTMLFromBundle()
}
func loadHTMLFromBundle(){
let url = Bundle.main.url(forResource: "index", withExtension: "html")
let urlRequest:URLRequest = URLRequest(url:url!)
self.webView.loadRequest(urlRequest)
}
func getComponents() {
guard let path = Bundle.main.path(forResource: "components", ofType: "txt") else { return }
let url = URL(fileURLWithPath: path)
let jsonData = try! Data(contentsOf: url)
tempComponentArray = try! JSONSerialization.jsonObject(with: jsonData, options: []) as! NSArray
print("jsonDict:\n \(tempComponentArray)")
do {
let jsonData = try JSONSerialization.data(withJSONObject: tempComponentArray, options: [])
//Convert back to string. Usually only do this for debugging
if let JSONString = String(data: jsonData, encoding: String.Encoding.utf8) {
print(JSONString)
let urlString:NSString = NSString(format: "myNewFunction(%@)", JSONString)
print("urlString: ",urlString)
self.templateFormStr = JSONString
print(self.templateFormStr)
let encodedStr = self.webView.stringByEvaluatingJavaScript(from: "myNewFunction('\(self.templateFormStr)')") **-----------> Here I passed or evaluate the data to javascript**
print("encodedStr: ",encodedStr!)
}
} catch {
print(error.localizedDescription)
}
}
@IBAction func loadJSBtnTapped(_ sender: Any) {
getComponents()
}
在这里我分享数组值,
[
{
"placeholder": "Enter the comments",
"input": true,
"showWordCount": false,
"label": "North",
"showCharCount": false,
"type": "textfield",
"allowMultipleMasks": false,
"key": "textField"
},
{
"placeholder": "Enter the comments",
"input": true,
"showWordCount": false,
"label": "South",
"showCharCount": false,
"type": "textfield",
"allowMultipleMasks": false,
"key": "south"
},
{
"placeholder": "Enter the comments",
"input": true,
"showWordCount": false,
"label": "East",
"showCharCount": false,
"type": "textfield",
"allowMultipleMasks": false,
"key": "east"
},
{
"placeholder": "Enter the comments",
"input": true,
"showWordCount": false,
"label": "West",
"showCharCount": false,
"type": "textfield",
"allowMultipleMasks": false,
"key": "west"
},
{
"placeholder": "Enter the comments",
"input": true,
"showWordCount": false,
"label": "Easements / Encroachments",
"showCharCount": false,
"type": "textfield",
"allowMultipleMasks": false,
"key": "easementsEncroachments"
},
{
"placeholder": "Enter the comments",
"input": true,
"showWordCount": false,
"label": "Soil ans Sub-Soil Conditions",
"showCharCount": false,
"type": "textfield",
"allowMultipleMasks": false,
"key": "soilAnsSubSoilConditions"
},
{
"placeholder": "Enter the comments",
"input": true,
"showWordCount": false,
"label": "Environmental Conditions",
"showCharCount": false,
"type": "textfield",
"allowMultipleMasks": false,
"key": "environmentalConditions"
}
]
在这里,我分享了我尝试过的 HTML 代码,
<!DOCTYPE html>
<html>
<p>Click the button to display an alert box.</p>
<script>
function myNewFunction(param) {
var arrayData = JSON.parse(param);
alert(arrayData)
}
</script>
<body>
<button onclick="myFunction()">Try it</button>
</body>
</html>
当 Display unparsed Json 意味着 alert 显示为空时,我附上了截图。