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 应用程序中的地理定位双重提示
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01