将从应用程序获取的地理位置传递给 UIWebView

Passing geolocation fetched from the app to the UIWebView(将从应用程序获取的地理位置传递给 UIWebView)

本文介绍了将从应用程序获取的地理位置传递给 UIWebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:将从应用程序获取的地理位置传递给 UIWebView

基础教程推荐