我向javascript函数发送一个值并获得结果。但是,我得到"错误域= WKErrorDomain代码= 5" JavaScript执行返回了不支持类型的结果" UserInfo = {NSLocalizedDescription = JavaScript执行返回不支持类型的结果}"在代码下运行时出错;
webView?.evaluateJavaScript("getURL()") { (result, error) in
if let error = error
{
print("\(error)");
return
}
if let song = result as? String
{
print("\(song)");
}
}
和javascript代码如下;
function getURL(){
CloudKit.configure({
containers: [{
// To use your own container, replace containerIdentifier and apiToken
containerIdentifier: 'iCloud.com.hello.myapp',
apiToken: 'myApiKey',
environment: 'development'
}]
});
console.log("cloudkitloaded");
var self = this;
console.log("get default container");
var container = CloudKit.getDefaultContainer();
console.log("set publicDB");
var publicDB = container.publicCloudDatabase;
self.items = ko.observableArray();
// Fetch public records
// self.fetchRecords = function() {
console.log("fetching records from " + publicDB);
var query = { recordType: 'Musics'};
// Execute the query.
return publicDB.performQuery(query).then(function(response) {
if(response.hasErrors) {
console.error(response.errors[0]);
return;
}
var records = response.records;
var numberOfRecords = records.length;
if (numberOfRecords === 0) {
console.error('No matching items');
return;
}
console.log(records.length + " records")
//console.log(records[0].fields.Song_File.value.downloadURL);
self.items(records);
myVar = records[emre].fields.Song_File.value.downloadURL;
document.getElementById('someId').innerHTML = myVar;
});
return "emre";
};
答案 0 :(得分:1)
尝试将您的JavaScript代码包装到setTimeout
中:
webView?.evaluateJavaScript("setTimeout(function() {getURL()}, 1000);")
答案 1 :(得分:1)
我遇到了同样的事情,搜索把我带到了这里。后来,我找到了答案:
这可能是由于您的JavaScript返回了nil
,然后将其报告为错误。只允许返回一个字符串。因此,只需将这些return
语句更改为return ''
或返回'error msg'`即可解决此问题。