2016-07-11 11 views
5

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

+0

Nur eine vage Idee ... Können Sie lokalen Speicher in webview aktivieren dann setzen Sie es und lesen Sie es später von Javascript? –

Antwort

4

Sie haben UIWebview Delegierten implementieren Die JavaScript-Funktion sollte nach webviewDidFinishLoad aufrufen.

4

Die Antwort, die Sie verknüpfen, ist für Apple TV, die sich sehr von iOS unterscheidet & UIWebView.

Bevor Sie versuchen, Javascript auszuführen, müssen Sie sicherstellen, dass die Webansicht geladen wurde (siehe webViewDidFinishLoad).

Wenn die Web-Ansicht bereit ist, und Sie haben Ihre Daten können Sie eine String erstellen, die die JavaScript enthält, die Sie ausführen und dann, dass der Ansicht Web-Pass:

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

webView.stringByEvaluatingJavaScriptFromString(javaScript)