将从应用中获取的地理位置传递给UIWebView [英] Passing geolocation fetched from the app to the UIWebView

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

问题描述

我正在开发iOS应用程序,并且在其中一个视图中,我希望将UIWebView包含一个包含地图的页面(地址为: https://carsharing.mvg-mobil.de )。该网站需要位置权限,因此只要用户看到该视图,就会显示一条警告: https:// carsharing.mvg-mobil.de/ 想要使用您当前的位置。确定/不允许。

在我的应用程序中,我有从CoreLocation框架中获取的用户的位置。有没有办法将此位置传递给此webview以避免此警报视图?是否有可能通过注入一些JavaScript代码或可能以某种方式在URL中包含地理位置?解决方案

好的,我发现解决方案在源代码 PhoneGap 如何将JavaScript代码注入到standrad webview中,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,
课程,
速度,
newLocation。 horizo​​ntalAccuracy,
newLocation.verticalAccuracy
];

NSString * jsCallBack = [NSString stringWithFormat:@navigator.geolocation.setLocation({timestamp:%d,%@});,epoch,coords];

[webView stringByEvaluatingJavaScriptFromString:jsCallBack];

希望其他人也可以从这段代码中获益。


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

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?

解决方案

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆