【问题标题】:Location permission alert on iPhone with Cordova带有 Cordova 的 iPhone 上的位置许可警报
【发布时间】:2015-05-31 20:57:22
【问题描述】:

我正在研究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 文件也不包含任何有关地理定位插件的数据。现在我再次安装了插件,它可以工作了。

【问题讨论】:

  • 不要在 deviceready 被触发后立即调用 getCurrentPosition。您是否尝试过 setTimeout 在延迟后调用 getCurrentPosition?
  • 是的,即使使用 setTimeout(function(){ navigator.geolocation.getCurrentPosition(onSuccess, onError); }, 60000);丑陋的 HTML 消息正在显示。
  • @Dev DOS 如何删除此错误
  • @nivritgupa 我不记得究竟是怎么回事,但我记得这是一个与地理定位插件有关的问题,所以:删除/添加地理定位插件:cordova plugin rm org.apache.cordova.geolocation cordova plugin add org.apache.cordova.geolocation 将 NSLocationWhenInUseUsageDescription 添加到 .plist。打开 /platforms/ios/{project}/{project}-Info.plist 并添加以下内容:&lt;key&gt;NSLocationWhenInUseUsageDescription&lt;/key&gt; &lt;string&gt;[App Name] would like to access your location when running and displayed.&lt;/string&gt;

标签: javascript ios iphone cordova-4


【解决方案1】:

与原始问题中的“编辑”相同,我必须删除旧版本的地理定位插件并添加新版本。然后我不得不删除/添加 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>

有关NSLocationWhenInUseUsageDescriptionNSLocationAlwaysUsageDescriptionNSLocationUsageDescription 的详细信息,请参阅此iOS Developer Library link

【讨论】:

  • 您好,如何使用 Cordova 在 iPhone 上禁用位置权限警报。
  • 我认为禁用警报是不可能的。据我了解,它作为一项基本安全功能硬连线到操作系统中,仅允许将此类私人信息(设备位置)传输到具有明确用户许可的第 3 方应用程序。
【解决方案2】:
  • 确保“cordova.js”文件存在于您的根文件夹中,只需将此 JS 文件包含在您访问此位置的 .html 文件中即可解决此问题。没有什么花哨的要求。如果此 js 文件丢失,navigator.geolocation 将调用基于浏览器的 HTML5 对象,因此会发出奇怪的警报。

【讨论】:

    【解决方案3】:

    您无法在模拟器或设备上禁用请求消息,但您可以通过向项目的 .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 (NSLocationUsageDescription

    【讨论】:

    • 你的意思是 config.xml 文件吗?因为在项目中找不到.plist文件
    • 如果你在本地构建你的cordova应用,你的plist file将是/{project}/platforms/ios/{project}/{project}-Info.plist
    • 我发现我的应用程序使用没有字符串值的 NSLocationWhenInUseUsageDescription 当我用我的自定义文本更改它时它会在我第一次运行我的应用程序时更改第一条消息,但第二条丑陋的消息仍然存在显示。我怎样才能改变它?
    猜你喜欢
    • 2010-12-13
    • 2015-06-24
    • 1970-01-01
    • 2015-12-31
    • 1970-01-01
    • 2016-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多