3

我正在研究一个cordova app我必须locate the user经纬度的问题。使用地理定位插件,它可以在 android 设备上正常工作,但它会显示一个警报,请求用户在iOS. 当我使用模拟器时,我收到以下警报消息:

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.

我见过谈论这个问题的话题,例如:thisthis,但提供的解决方案都不适合我。

这是科尔多瓦示例页面:

<!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>

有什么方法可以更改警报的文本或禁用此警报?

-编辑 - -

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

4

3 回答 3

5

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

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

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

二、移除/添加iOS平台:

cordova platform rm ios
cordova platform add ios

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

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

有关与vs的详细信息,请参阅此iOS 开发人员库链接NSLocationWhenInUseUsageDescriptionNSLocationAlwaysUsageDescriptionNSLocationUsageDescription

于 2015-06-09T20:42:32.713 回答
2
  • 确保“cordova.js”文件存在于您的根文件夹中,只需将此 JS 文件包含在您访问此位置的 .html 文件中即可解决此问题。没有什么花哨的要求。如果此 js 文件丢失,navigator.geolocation 将调用基于浏览器的 HTML5 对象,因此会发出奇怪的警报。
于 2016-11-30T14:49:48.060 回答
1

您无法在模拟器或设备上禁用请求消息,但您可以通过将属性添加到项目的 .plist 来自定义它。

对于 iOS 8,如果您的应用程序请求在后台使用位置的权限,您需要以下键(将字符串值设置为您喜欢的任何值):

<key>NSLocationAlwaysUsageDescription</key>
<string>My app requires constant access to your location, even when the screen is off.</string>

如果您的应用仅在前台使用位置信息,请添加以下键:

<key>NSLocationWhenInUseUsageDescription</key>
<string>My app requires access to your location when the screen is on and the app is displayed.</string>

对于旧版本的 iOS (<=7),您需要使用NSLocationUsageDescription

于 2015-03-28T20:33:11.960 回答