Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Geolocation from Swift 2 ViewController to Javascript Method

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

like image 740
applecrusher Avatar asked Jul 11 '16 22:07

applecrusher


2 Answers

You have to implement UIWebview delegate.Javascript function should call after 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)
        }
like image 96
Bhoomi Jagani Avatar answered Nov 18 '22 08:11

Bhoomi Jagani


The answer you link to is for Apple TV, which is very different to iOS & UIWebView.

Before you try to run any javascript you need to be sure that the web view has finished loading (see webViewDidFinishLoad).

When the web view is ready and you have your details you can create a String which contains the javascript you want to execute and then pass that to the web view:

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

webView.stringByEvaluatingJavaScriptFromString(javaScript)
like image 4
Wain Avatar answered Nov 18 '22 08:11

Wain