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