How to prevent double prompt for geolocation in Phonegap app?(如何防止 Phonegap 应用程序中的地理定位双重提示?)
问题描述
我为 iPhone 创建了一个 PhoneGap 应用,它通过 webview 中的 JavaScript 使用地理定位.
I created a PhoneGap app for iPhone that uses geolocation via JavaScript inside webview.
当我第一次运行该应用程序时,它会提示我允许此应用程序的地理定位.
When I run the app the first time, it'll prompt me to allow geolocation for this app.
当我点击ok"时,它会再次提示我同样的问题,但这次它指出index.html"需要使用地理位置的权限.
When I hit "ok", it'll prompt me again with the same question but this time it states that "index.html" wants permission to use geolocation.
这是有道理的,因为 iOS 可能第一次需要允许应用程序本身的地理定位权限,而浏览器第二次需要权限.
That makes sense because iOS probably wants permission to allow geolocation for the app itself for the first time and the 2nd time the browser wants permission.
但是,因为不会带来出色的用户体验:
However, since doesn't lead to a great user experience:
如何防止这种双重提示?(如果可以防止第二个提示就足够了)
How can I prevent this double prompt? (I'd be enough if the 2nd prompt could be prevented)
推荐答案
我找到了问题的原因.
对 navigator.geolocation.getCurrentPosition(onsuccess, onerror) 的调用发生在 Phonegap 完全加载之前.
The call to navigator.geolocation.getCurrentPosition(onsuccess, onerror) happens before Phonegap was fully loaded.
这意味着 webview 的地理定位调用(而不是通过 PhoneGap 的本地调用)被触发,这将再次请求许可(这确实有意义).将其与智能手机上的普通 Safari 浏览器进行比较.它会要求每个新网站的地理位置许可.应用启动时通过PhoneGap加载index.html也是一样.
This means that the geolocation call of webview (and not a native call via PhoneGap) is being triggered which will again ask for permission (which does make sense). Compare it to the normal Safari browser on your Smartphone. It'll ask for geolocation permission for every new website. It's the same when loading index.html via PhoneGap on application startup.
但是,解决方案是等待在 PhoneGap 完全加载时触发的 deviceready 事件:
However, the solution is to wait for the deviceready event which gets fired when PhoneGap has fully loaded:
document.addEventListener("deviceready", function(){
navigator.geolocation.getCurrentPosition(onsuccess, onerror, params);
}, false);
这将使 PhoneGap API 可用,它会覆盖浏览器的默认 HTML5 地理定位调用,并通过本机调用(您已在第一个提示中接受)获取设备的地理位置.
This will make the PhoneGap API available which overwrites the default HTML5 gelocation call of the browser and get the device's geo location via a native call (which you already accepted in the first prompt).
这会起作用,因为 PhoneGap 的 API 调用与 HTML5 的标准 W3C 调用相同:http://docs.phonegap.com/en/2.2.0/cordova_geolocation_geolocation.md.html#Geolocation
This will work because PhoneGap's API calls are identical to the standard W3C call for HTML5: http://docs.phonegap.com/en/2.2.0/cordova_geolocation_geolocation.md.html#Geolocation
这篇关于如何防止 Phonegap 应用程序中的地理定位双重提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何防止 Phonegap 应用程序中的地理定位双重提示
基础教程推荐
- 新的@SystemApi 注解是什么意思,和@hide 有什么区别 2022-01-01
- 在 iOS8 中无法获得正确的键盘高度值 2022-01-01
- 在 Android 模拟器中激活网络位置提供程序? 2022-01-01
- Android:STATE_SELECTED不工作 2022-01-01
- 如何将多个组件添加到 PickerView? 2022-01-01
- 突出显示朗读文本(在 iPhone 的故事书类型应用程序中) 2022-01-01
- - (CGRect)convertRect:(CGRect)rect toView:(UIView *)view 如何工作 2022-01-01
- 我的 UIImageView 的任务 2022-01-01
- 可可/目标C(OSX不是iOS)从子对象访问父对象 2022-01-01
- 在 appComponent dagger 2 中动态添加测试模块? 2022-01-01
