调用方法时找不到变量错误

调用方法时找不到变量错误

本文介绍了Swift WKWebView:调用方法时找不到变量错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 GPS 坐标传递给方法调用 setIOSNativeAppLocation.我在下面有以下代码,但出现此错误:

I am trying to pass GPS coordinates to a method call setIOSNativeAppLocation. I have the following code below but I am getting this error:

A JavaScript exception occurred"
UserInfo={WKJavaScriptExceptionLineNumber=1, WKJavaScriptExceptionMessage=
ReferenceError:
Can't find variable: setIOSNativeAppLocation, WKJavaScriptExceptionSourceURL=
http://mywebsite.com, NSLocalizedDescription=
A JavaScript exception occurred, WKJavaScriptExceptionColumnNumber=24})

我不确定它是否与语法或其他任何事情有关.如果有人知道我做错了什么,我将不胜感激.

I am not sure if it has something to do with the syntax or anything else. If someone knows what I am doing wrong I would greatly appreciate it.

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {



   if(myCurrentLoc != nil){


      let lat = myCurrentLoc.coordinate.latitude
      let lon = myCurrentLoc.coordinate.longitude


      print(lat);
      print(lon);

      webView.evaluateJavaScript("setIOSNativeAppLocation(\(lat), \(lon));")  { (result, error) in
         guard error == nil else {
            print("there was an error")
            print(error)
            return
         }

      }
   }
}

推荐答案

我认为这可以帮助你 - 我们在这里传递参数的方式.

I think this can help you out- The way we are passing parameter here.

我们应该把它放在单引号中('\(lat)', '\(lon)') 否则它就像一个变量.

we should put it in single quote('\(lat)', '\(lon)') else it will be like a variable.

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
if(myCurrentLoc != nil){


  let lat = myCurrentLoc.coordinate.latitude
  let lon = myCurrentLoc.coordinate.longitude


  print(lat);
  print(lon);

  webView.evaluateJavaScript("setIOSNativeAppLocation('\(lat)', '\(lon)');")  { (result, error) in
     guard error == nil else {
        print("there was an error")
        print(error)
        return
     }
   }

 }

}

这篇关于Swift WKWebView:调用方法时找不到变量错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 12:51