ios WkWebView Evaluatejavascript返回不支持的类型错误

inb24sb2  于 2023-03-09  发布在  iOS
关注(0)|答案(3)|浏览(710)

我正在向javascript函数发送一个值并获得一个结果。但是,当运行以下代码时,我收到“Error Domain=WKErrorDomain Code=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";

  };
nc1teljy

nc1teljy1#

试着把你的javascript代码打包成setTimeout

webView?.evaluateJavaScript("setTimeout(function() {getURL()}, 1000);")
qvtsj1bj

qvtsj1bj2#

我遇到了同样的事情,搜索把我带到了这里。后来,我找到了答案:
这可能是由于从javascript返回nil,然后报告为错误。只允许返回字符串。因此只需将这些return语句更改为return ''return 'error msg'就可以修复它。

3wabscal

3wabscal3#

使用匿名函数

webView.evaluateJavaScript("""
    (function() {
        getURL();
    })();
""")

相关问题