iPhone 上使用 Cordova 的位置权限提醒
Location permission alert on iPhone with Cordova
我正在研究 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.
我看到过像这样讨论这个问题的话题:this and this但是 none 所提供的解决方案对我有用。
这是科尔多瓦示例页面:
<!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
文件也不包含任何关于地理定位插件的数据。现在我已经重新安装了这个插件并且可以使用了。
您无法在模拟器或设备上禁用请求消息,但您可以通过向项目的 .plist 添加 属性 来自定义它。
对于 iOS8,如果您的应用程序请求在后台使用位置的权限,您需要以下键(将字符串值设置为您喜欢的任何值):
<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
与原问题中的 "edit" 相同,我不得不删除旧版本的地理定位插件并添加新版本。然后我不得不 remove/add Cordova iOS 平台。只有这样我才能将 NSLocationWhenInUseUsageDescription
添加到 .plist 文件中,因为 DaveAlden 在他的回答中提到了成功。
首先,remove/add 地理定位插件:
cordova plugin rm org.apache.cordova.geolocation
cordova plugin add org.apache.cordova.geolocation
二、remove/addiOS平台:
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>
有关 NSLocationWhenInUseUsageDescription
与 NSLocationAlwaysUsageDescription
与 NSLocationUsageDescription
的详细信息,请参阅此 iOS Developer Library link。
- 确保 "cordova.js" 文件存在于您的根文件夹中,只需将此 JS 文件包含在您访问此位置的 .html 文件中即可解决此问题。不需要花哨的东西。如果此 js 文件丢失,navigator.geolocation 将调用基于浏览器的 HTML5 对象,因此会出现奇怪的警报。
我正在研究 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.
我看到过像这样讨论这个问题的话题:this and this但是 none 所提供的解决方案对我有用。
这是科尔多瓦示例页面:
<!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
文件也不包含任何关于地理定位插件的数据。现在我已经重新安装了这个插件并且可以使用了。
您无法在模拟器或设备上禁用请求消息,但您可以通过向项目的 .plist 添加 属性 来自定义它。
对于 iOS8,如果您的应用程序请求在后台使用位置的权限,您需要以下键(将字符串值设置为您喜欢的任何值):
<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
与原问题中的 "edit" 相同,我不得不删除旧版本的地理定位插件并添加新版本。然后我不得不 remove/add Cordova iOS 平台。只有这样我才能将 NSLocationWhenInUseUsageDescription
添加到 .plist 文件中,因为 DaveAlden 在他的回答中提到了成功。
首先,remove/add 地理定位插件:
cordova plugin rm org.apache.cordova.geolocation
cordova plugin add org.apache.cordova.geolocation
二、remove/addiOS平台:
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>
有关 NSLocationWhenInUseUsageDescription
与 NSLocationAlwaysUsageDescription
与 NSLocationUsageDescription
的详细信息,请参阅此 iOS Developer Library link。
- 确保 "cordova.js" 文件存在于您的根文件夹中,只需将此 JS 文件包含在您访问此位置的 .html 文件中即可解决此问题。不需要花哨的东西。如果此 js 文件丢失,navigator.geolocation 将调用基于浏览器的 HTML5 对象,因此会出现奇怪的警报。