使用Cordova在iPhone上的位置许可警报 [英] Location permission alert on iPhone with Cordova

查看:136
本文介绍了使用Cordova在iPhone上的位置许可警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 cordova app ,我必须找到用户纬度和经度。
使用geolocation插件,它可以在Android设备上正常工作,但它会在 iOS 中显示要求用户许可的警报。当我使用模拟器时,我收到此警告消息:

I'm working on a cordova app on which I have to locate the user latitude and longitude. Using the geolocation plugin, it works fine on android devices but it display an alert asking for permission from user in iOS. When I used the simulator I get this alert message:

Users/user/Library/Developer/CoreSimulator/Devices/783A2EFD-2976-448C-8E4E-841C985D337D/data/Containers/Bundle/Application/EFC846BB-4BA3-465C-BD44-575582E649FC/app_name.app/www/index.html would like to use your current location.

我看过这个问题的话题如下: this this 但是所提供的解决方案都不适合我。

I have seen topic talking about this problem like: this and thisbut none of the provided solutions works for me.

这是cordova示例页面:

this is the cordova example page:

<!DOCTYPE html>
 <html>
<head>
<title>Device Properties Example</title>

<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">

document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
    navigator.geolocation.getCurrentPosition(onSuccess, onError);
}
function onSuccess(position) {
    var element = document.getElementById('geolocation');
    element.innerHTML = 'Latitude: '           + position.coords.latitude              + '<br />' +
                        'Longitude: '          + position.coords.longitude             + '<br />' +
                        'Altitude: '           + position.coords.altitude              + '<br />' +
                        'Accuracy: '           + position.coords.accuracy              + '<br />' +
                        'Altitude Accuracy: '  + position.coords.altitudeAccuracy      + '<br />' +
                        'Heading: '            + position.coords.heading               + '<br />' +
                        'Speed: '              + position.coords.speed                 + '<br />' +
                        'Timestamp: '          + position.timestamp                    + '<br />';
}

function onError(error) {
    alert('code: '    + error.code    + '\n' +
          'message: ' + error.message + '\n');
}

</script>
</head>
<body>
<p id="geolocation">Finding geolocation...</p>
</body>

有没有办法改变警报文本或禁用此警报?

Is there any way to change the text of the alert or to disable this alert?

-edit ---

-edit---

我找到了我的问题的根源。我删除了geolocation插件并添加了几次,因为当我添加插件时,我没有找到一个名为 geolocation插件的文件夹,就像其他插件一样。甚至 cordova_plugin.js 文件也不包含任何有关地理定位插件的数据。现在我再次安装了插件并且它可以工作。

I have found the source of my problem. I removed the geolocation plugin and add it several times because when I have added the plugin I haven't found a folder with the name of the geolocation plugin like the other plugins. Even the cordova_plugin.js file doesn't contain any data about geolocation plugin. Now I have installed the plugin again and it works.

推荐答案

与原始问题中的编辑相同,我不得不删除旧版本的geolocation插件并添加新版本。然后我不得不删除/添加Cordova iOS平台。只有这样我才能将 NSLocationWhenInUseUsageDescription 添加到.plist文件中,如 DaveAlden 在他的回答中提到了成功。

Same as the "edit" in the original question, I had to remove the old version of the geolocation plugin and add the new one. Then I had to remove/add the Cordova iOS platform. Only then could I add NSLocationWhenInUseUsageDescription to the .plist file as DaveAlden mentions in his answer with success.

首先,删除/添加地理位置插件:

First, remove/add the geolocation plugin:

cordova plugin rm org.apache.cordova.geolocation
cordova plugin add org.apache.cordova.geolocation

其次,删除/添加iOS平台:

Second, remove/add the iOS platform:

cordova platform rm ios
cordova platform add ios

最后,添加 NSLocationWhenInUseUsageDescription 到.plist。打开 / platforms / ios / {project} / {project} -Info.plist 并添加以下内容:

Last, add NSLocationWhenInUseUsageDescription to the .plist. Open /platforms/ios/{project}/{project}-Info.plist and add the following:

<key>NSLocationWhenInUseUsageDescription</key>
<string>[App Name] would like to access your location when running and displayed.</string>

请参阅 iOS Developer Library链接,了解有关<$ c的详细信息$ c> NSLocationWhenInUseUsageDescription 与 NSLocationAlwaysUsageDescription NSLocationUsageDescription

See this iOS Developer Library link for detailed information regarding NSLocationWhenInUseUsageDescription versus NSLocationAlwaysUsageDescription versus NSLocationUsageDescription.

这篇关于使用Cordova在iPhone上的位置许可警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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