I'm sending a value to javascript function and get a result. However, I'm getting "Error Domain=WKErrorDomain Code=5 "JavaScript execution returned a result of an unsupported type" UserInfo={NSLocalizedDescription=JavaScript execution returned a result of an unsupported type}" error when run below code;
webView?.evaluateJavaScript("getURL()") { (result, error) in
if let error = error
{
print("\(error)");
return
}
if let song = result as? String
{
print("\(song)");
}
}
and javascript code as follows;
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";
};
Try to wrap your javascript code into setTimeout
:
webView?.evaluateJavaScript("setTimeout(function() {getURL()}, 1000);")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With