Passing geolocation from Swift 2 ViewController to Javascript method

The code below can get the geolocation and print it. I got this based on some online tutorials and look at Swift Documents. I want to pass geolocation as strings from Swift 2 to Javascript. I can get GeoLocations, I don’t know how to pass these lines to my Javascript Code in Webview.

Below is the code:

@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 site using this method:

 function setIOSNativeAppLocation(lat , lon){ nativeAppLat = lat; nativeAppLon = lon; alert(nativeAppLat); alert(nativeAppLon); } 

I examined another question tagged with a 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 do not have appDelegate, and I want to make these changes directly from this view. So I get "using an unresolved appDelegate id".

+6
source share
2 answers

You need to implement the UIWebview delegate. The 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) } 
+5
source

The answer you are referring to is Apple TV, which is very different from iOS and UIWebView .

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

When the web view is ready and you have the data, you can create a String that contains the javascript that you want to execute, and then pass it to the web view:

 let javaScript = "setIOSNativeAppLocation(\(myCurrentLoc.coordinate.latitude), \(myCurrentLoc.coordinate.longitude))" webView.stringByEvaluatingJavaScriptFromString(javaScript) 
+4
source

All Articles