Passing Geolocation from Swift 2 ViewController to Javascript Method

时间:2016-07-11 22:34:27

标签: javascript ios swift uiwebview

The code below is able to get the geolocation and prints it out. I got this based on some tutorials online and looking at Swift Documents. I want to pass geolocations in the form of Strings from Swift 2 to Javascript. I am able to get the GeoLocations, I do not know how to pass these Strings to my Javascript Code in the Webview.

Below is the Code I have:

@IBOutlet weak var Webview: UIWebView!

let locMgr = CLLocationManager()

override func viewDidLoad() {

    super.viewDidLoad()
    loadAddressURL()
    locMgr.desiredAccuracy = kCLLocationAccuracyBest
    locMgr.requestWhenInUseAuthorization()
    locMgr.startUpdatingLocation()
    locMgr.delegate = self //necessary



}

func locationManager(manager: CLLocationManager , didUpdateLocations locations: [CLLocation]){
    let myCurrentLoc = locations[locations.count-1]
    var myCurrentLocLat:String = "\(myCurrentLoc.coordinate.latitude)"
    var myCurrentLocLon:String = "\(myCurrentLoc.coordinate.longitude)"

    print(myCurrentLocLat)
    print(myCurrentLocLon)

    //pass to javascript here by calling setIOSNativeAppLocation

}

I have javascript on my website with this method:

function setIOSNativeAppLocation(lat , lon){

  nativeAppLat = lat;
  nativeAppLon = lon;
  alert(nativeAppLat);
  alert(nativeAppLon);

}

I have looked at another question labelled Pass variable from Swift to Javascript with the following solution:

func sendSomething(stringToSend : String) {
    appController?.evaluateInJavaScriptContext({ (context) -> Void in

       //Get a reference to the "myJSFunction" method that you've implemented in JavaScript
       let myJSFunction = evaluation.objectForKeyedSubscript("myJSFunction")

       //Call your JavaScript method with an array of arguments
       myJSFunction.callWithArguments([stringToSend])

       }, completion: { (evaluated) -> Void in
          print("we have completed: \(evaluated)")
    })
}

However I have no appDelegate, and I want to directly from this view make these changes. So I get a "use of unresolved identifier appDelegate".

2 个答案:

答案 0 :(得分:5)

你必须实现UIWebview delegate.Javascript函数应该在webviewDidFinishLoad之后调用。

override func viewDidLoad() {
     super.viewDidLoad()
     // Do any additional setup after loading the view, typically from a nib.
     let url = NSURL (string: URLSTRING);
     let requestObj = NSURLRequest(URL: url!);
webView.delegate = self
    webView.loadRequest(requestObj);
  }

    func webViewDidFinishLoad(webView: UIWebView) {
            let javaScriptStr = "setIOSNativeAppLocation(\(myCurrentLoc.coordinate.latitude), \(myCurrentLoc.coordinate.longitude))"

            webView.stringByEvaluatingJavaScriptFromString(javaScriptStr)
        }

答案 1 :(得分:4)

您链接的答案适用于Apple TV,这与iOS& UIWebView

在尝试运行任何javascript之前,您需要确保Web视图已完成加载(请参阅webViewDidFinishLoad)。

当网络视图准备就绪并且您有详细信息时,您可以创建一个String,其中包含您要执行的javascript,然后将其传递给网络视图:

let javaScript = "setIOSNativeAppLocation(\(myCurrentLoc.coordinate.latitude), \(myCurrentLoc.coordinate.longitude))"

webView.stringByEvaluatingJavaScriptFromString(javaScript)