Der folgende Code ist in der Lage, den Geolocation zu ermitteln und auszudrucken. Ich habe dies basierend auf einigen Tutorials online und Blick auf Swift Documents. Ich möchte Geolocations in Form von Strings von Swift 2 an Javascript weitergeben. Ich bin in der Lage, die GeoLocations zu bekommen, ich weiß nicht, wie ich diese Strings an meinen Javascript-Code im Webview weitergeben soll.Übergeben von Geolocation von Swift 2 ViewController an Javascript Methode
Unten ist der Code ich habe:
@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
}
ich habe Javascript auf meiner Website mit dieser Methode:
function setIOSNativeAppLocation(lat , lon){
nativeAppLat = lat;
nativeAppLon = lon;
alert(nativeAppLat);
alert(nativeAppLon);
}
ich bei einer anderen Frage Pass variable from Swift to Javascript mit folgenden Lösung markiert ausgesehen habe:
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)")
})
}
Allerdings habe ich keine AppDelegate, und ich möchte direkt von t seine Ansicht macht diese Änderungen. So bekomme ich eine "Verwendung von nicht aufgelösten Bezeichner appDelegate".
Nur eine vage Idee ... Können Sie lokalen Speicher in webview aktivieren dann setzen Sie es und lesen Sie es später von Javascript? –