以下のコードはジオロケーションを取得して出力します。オンラインチュートリアルやスウィフトドキュメントを参考にしています。 Swift 2からJavascriptにストリングの形式でジオロケーションを渡したいと思います。私はGeoLocationsを取得することができます、私はどのようにWebviewで私のJavascriptコードにこれらの文字列を渡すか分からない。以下はSwift 2 ViewControllerからジオロケーションをJavascriptメソッドに渡す
私が持っているコードです:
@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
}
私はこの方法で私のウェブサイト上でJavaScriptを持っている:
:私は、以下の溶液で Pass variable from Swift to Javascriptラベルされた別の質問を見てきましたfunction setIOSNativeAppLocation(lat , lon){
nativeAppLat = lat;
nativeAppLon = lon;
alert(nativeAppLat);
alert(nativeAppLon);
}
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)")
})
}
私はappDelegateを持っていません。私は直接彼の見解はこれらの変更を行います。だから私は "未解決の識別子appDelegateの使用"を取得します。
ちょっと漠然としたアイデア... webviewでローカルストレージを有効にしてから設定し、後でjavascriptから読み込むことはできますか? –