問題描述
我正在開發 iOS 應用程序,我想在其中一個視圖中包含 UIWebView 和包含地圖的頁面(地址是:https://carsharing.mvg-mobil.de).該網站需要位置許可,因此一旦用戶看到此視圖,就會出現一條警報:https://carsharing.mvg-mobil.de/ 想使用您當前的位置.OK/不允許".
I'm developing the iOS app and in one of the views I would like to include UIWebView with a page that contains a map (the address is: https://carsharing.mvg-mobil.de). The website requires location permission so as soon as the user is presented with this view, an alert appears: "https://carsharing.mvg-mobil.de/ would like to use your current location. OK/Don't allow".
在我的應用程序中,我從 CoreLocation 框架中獲取了用戶的位置.有沒有辦法將此位置傳遞給此 web 視圖以避免此警報視圖?是否可以通過注入一些 JavaScript 代碼或者以某種方式在 URL 中包含地理位置?
In my app I have the location of the user fetched from the CoreLocation framework. Is there a way to pass this location to this webview to avoid this alert view? Would it be somehow possible by injecting some JavaScript code or maybe somehow including geolocation in the URL?
推薦答案
好的,我在PhoneGap 如何使用從 CoreLocation 獲取的位置將 JavaScript 代碼注入標準 webview:
OK, I found the solution in the source code of PhoneGap how to inject JavaScript code to a standrad webview with the location fetched from CoreLocation:
int epoch = [newLocation.timestamp timeIntervalSince1970];
float course = -1.0f;
float speed = -1.0f;
NSString* coords = [NSString stringWithFormat:@"coords: { latitude: %f, longitude: %f, altitude: %f, heading: %f, speed: %f, accuracy: %f, altitudeAccuracy: %f }",
newLocation.coordinate.latitude,
newLocation.coordinate.longitude,
newLocation.altitude,
course,
speed,
newLocation.horizontalAccuracy,
newLocation.verticalAccuracy
];
NSString * jsCallBack = [NSString stringWithFormat:@"navigator.geolocation.setLocation({ timestamp: %d, %@ });", epoch, coords];
[webView stringByEvaluatingJavaScriptFromString:jsCallBack];
希望其他人也可以從這段代碼中獲利.
Hopefully somebody else can profit from this code as well.
這篇關于將從應用程序獲取的地理位置傳遞給 UIWebView的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!