如何防止双重提示在Phonegap应用程序中的地理位置? [英] How to prevent double prompt for geolocation in Phonegap app?

查看:162
本文介绍了如何防止双重提示在Phonegap应用程序中的地理位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当我第一次运行应用程序时,它会提示我允许地理位置



当我点击确定,它会提示我再次提出同样的问题,但这一次它说index.html需要权限使用地理位置。



这是有道理的,因为iOS可能需要权限允许应用程序本身的地理位置第一次和浏览器第二次需要权限。



但是,由于不会带来很好的用户体验:



如何防止出现这种双重提示?

解决方案

我找到了问题的原因。



在Phonegap完全加载之前调用 navigator.geolocation.getCurrentPosition(onsuccess,onerror)



这意味着 webview (而不是通过PhoneGap的本地调用)的地理位置调用被触发,这将再次请求权限(这是有意义的)。将其与智能手机上的普通Safari浏览器进行比较。它会要求每个新网站的地理位置权限。在应用程序启动时通过PhoneGap加载index.html时也是如此。



然而,解决方案是等待deviceready事件,当PhoneGap完全加载时触发: / p>

  document.addEventListener(deviceready,function(){
navigator.geolocation.getCurrentPosition(onsuccess,onerror,params );
},false);

这将使PhoneGap API可用,覆盖浏览器的默认HTML5 gelocation调用,



这将工作,因为PhoneGap的API调用与HTML5的标准W3C调用是相同的:

a href =http://docs.phonegap.com/en/2.2.0/cordova_geolocation_geolocation.md.html#Geolocation> http://docs.phonegap.com/en/2.2.0/cordova_geolocation_geolocation.md.html #Geolocation


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.

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.

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)

解决方案

I found the cause for the issue.

The call to navigator.geolocation.getCurrentPosition(onsuccess, onerror) happens before Phonegap was fully loaded.

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.

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);

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

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

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