我正在开发一个带有phonegap的IOS应用程序,我希望在加载时获得用户对位置服务的许可。问题是,在我恢复应用程序(按住主页并再次打开应用程序)或等待几分钟后才会出现要求许可的警报,这是不可接受的。
我正在使用deviceready
事件:
document.addEventListener("deviceready", deviceReady, false);
function deviceReady() {
navigator.geolocation.getCurrentPosition(onLocationSuccess,
onLocationError
);
}
如果我不等待设备准备好,警报就会显示正常,但我会收到一条消息,提示如下:
/var/mobile/Applications/XXXX-XXXX-XXXX-XXXXXXXXXXX/AppName.app/www/index.html would like to use your current location
有什么想法让这个工作吗?
答案 0 :(得分:2)
我正在与这个问题争斗几天,我终于得到了解决方案。好像它不是插件也不是corvoda版本的责任。尝试将此内容 - 安全政策条目添加到 index.html 内部主题部分:
<meta http-equiv="Content-Security-Policy" content="default-src * gap://ready file:; style-src 'self' 'unsafe-inline'; img-src 'self'
数据:; script-src *&#39; unsafe-inline&#39; &#39;不安全-EVAL&#39;&#34;&GT;
重要的部分是
default-src * gap:// ready file :;
gap:仅在iOS上使用(使用UIWebView时),并且是JS-&gt;原生通信所必需的。
我希望它有所帮助。
答案 1 :(得分:0)
虽然你已经解决了这个问题,但是以后将会帮助其他人。
要在 iOS 上显示权限对话框,您必须在 config.xml 中进行以下配置。
<gap:config-file platform="ios" parent="NSLocationAlwaysUsageDescription" overwrite="false">
<array>
<string>NSLocationAlwaysUsageDescription</string>
</array>
</gap:config-file>
我使用它并且效果很好。
答案 2 :(得分:0)
这个解决方案非常适合我:
<gap:config-file platform="ios" parent="NSLocationAlwaysUsageDescription" overwrite="false">
<array>
<string>NSLocationAlwaysUsageDescription</string>
</array>
</gap:config-file>
答案 3 :(得分:0)
iOS 上的权限对话框在 config.xml
中使用此代码<gap:config-file platform="ios" parent="NSLocationAlwaysUsageDescription" overwrite="false">
<array>
<string>NSLocationAlwaysUsageDescription</string>
</array>
@AAhad针对您的问题,当用户关闭对话框或提醒时,您可以检查该选项是否可用,如果您需要激活GPS,可以根据您的请求与用户通话。
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
navigator.geolocation.getCurrentPosition(onSuccess, onError);
}
// onSuccess Geolocation
//
function onSuccess(position) {
//Code for your GPS
}
// onError Callback receives a PositionError object or if your user close de dialog or cancel de permission
//
function onError(error) {
alert('Error please check your GPS on Settings: ' + error.code + '\n' +
'Please Active Your GPS' + error.message + '\n');
}