javascript 带有 Cordova 的 iPhone 上的位置权限警报
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29302242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Location permission alert on iPhone with Cordova
提问by Dev DOS
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:
我正在研究一个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.
I have seen topic talking about this problem like: thisand thisbut none of the provided solutions works for me.
我见过这样的话题谈论这个问题:这个和这个但没有提供的解决方案适合我。
this is the cordova example page:
这是cordova示例页面:
<!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---
-编辑 - -
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 plugin
其他插件名称相同的文件夹。甚至该cordova_plugin.js
文件也不包含有关地理定位插件的任何数据。现在我再次安装了插件并且它可以工作。
回答by trailing slash
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 DaveAldenmentions in his answer with success.
与原始问题中的“编辑”相同,我必须删除旧版本的地理定位插件并添加新版本。然后我不得不删除/添加 Cordova iOS 平台。只有这样我NSLocationWhenInUseUsageDescription
才能像DaveAlden在他的回答中提到的那样成功地添加到 .plist 文件中。
First, remove/add the geolocation plugin:
首先,删除/添加地理定位插件:
cordova plugin rm org.apache.cordova.geolocation
cordova plugin add org.apache.cordova.geolocation
Second, remove/add the iOS platform:
二、移除/添加iOS平台:
cordova platform rm ios
cordova platform add ios
Last, add NSLocationWhenInUseUsageDescription
to the .plist. Open /platforms/ios/{project}/{project}-Info.plist
and add the following:
最后,添加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>
See this iOS Developer Library linkfor detailed information regarding NSLocationWhenInUseUsageDescription
versus NSLocationAlwaysUsageDescription
versus NSLocationUsageDescription
.
看到这个的iOS开发者库的链接有关的详细信息NSLocationWhenInUseUsageDescription
与NSLocationAlwaysUsageDescription
对比NSLocationUsageDescription
。
回答by DaveAlden
You can't disable the request message, either in the emulator or on the device, but you can customise it by adding a property to your project's .plist.
您无法在模拟器或设备上禁用请求消息,但您可以通过向项目的 .plist 添加属性来自定义它。
For iOS 8, if your app requests permission to use location in the background, you want the following key (set the string value to anything you like):
对于 iOS 8,如果您的应用请求在后台使用位置的权限,您需要以下键(将字符串值设置为您喜欢的任何值):
<key>NSLocationAlwaysUsageDescription</key>
<string>My app requires constant access to your location, even when the screen is off.</string>
If your app only uses location while in the foreground, then add the following key:
如果您的应用仅在前台使用位置,请添加以下键:
<key>NSLocationWhenInUseUsageDescription</key>
<string>My app requires access to your location when the screen is on and the app is displayed.</string>
For older versions of iOS (<=7) you need to use NSLocationUsageDescription
对于旧版本的 iOS (<=7),您需要使用 NSLocationUsageDescription
回答by mahipalz
- Make sure "cordova.js" file exists in your root folder and just by including this JS file in your .html file where you are accessing this location will resolve this issue. NOTHING FANCY REQUIRED. If this js file is missing, the navigator.geolocation will call the HTML5 object which is browser based and hence the weird alert.
- 确保“cordova.js”文件存在于您的根文件夹中,只需将此 JS 文件包含在您访问此位置的 .html 文件中即可解决此问题。不需要任何花哨的东西。如果缺少这个 js 文件,navigator.geolocation 将调用基于浏览器的 HTML5 对象,因此会出现奇怪的警报。