我正在开发 iOS 应用程序,在其中一个视图中,我想将 UIWebView 包含在包含地图的页面中(地址是:https://carsharing.mvg-mobil.de)。该网站需要位置权限,因此一旦用户看到此视图,就会出现警报:“https://carsharing.mvg-mobil.de/想要使用您当前的位置。好的/不允许”。
在我的应用程序中,我从 CoreLocation 框架获取了用户的位置。有没有办法将此位置传递给此 webview 以避免出现此警报视图?是否可以通过注入一些 JavaScript 代码或者以某种方式在 URL 中包含地理位置来实现?
好的,我在源码中找到了解决方案PhoneGap如何使用从 CoreLocation 获取的位置将 JavaScript 代码注入到标准 Web 视图中:
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];
希望其他人也可以从这段代码中受益。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)