Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WkWebView Evaluatejavascript returns unsupported type error

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";

  };
like image 827
Emre Önder Avatar asked Mar 19 '17 13:03

Emre Önder


1 Answers

Try to wrap your javascript code into setTimeout:

webView?.evaluateJavaScript("setTimeout(function() {getURL()}, 1000);")
like image 177
Dhiroo Verma Avatar answered Sep 19 '22 12:09

Dhiroo Verma