cordovaiPhone上的位置权限提醒
我正在开发一个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.
我已经看到了这个问题的话题: 这个和这个,但提供的解决scheme没有为我工作。
这是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>
有什么方法可以更改警报文本或禁用此警报?
-编辑 – –
我发现了我的问题的根源。 我删除了地理位置插件,并添加了几次,因为当我添加插件时,我没有find像其他插件geolocation plugin
名称的文件夹。 即使cordova_plugin.js
文件也不包含有关地理位置插件的任何数据。 现在我已经安装了插件,它的工作原理。
与原始问题中的“编辑”相同,我必须删除旧版本的地理位置插件并添加新版本。 然后,我不得不删除/添加cordovaiOS平台。 只有这样我才可以添加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>
请参阅此iOS开发人员库链接 ,了解有关NSLocationWhenInUseUsageDescription
详细信息时使用使用说明与NSLocationAlwaysUsageDescription
与NSLocationUsageDescription
。
您不能在模拟器或设备上禁用请求消息,但可以通过向项目的.plist添加属性来自定义它。
对于iOS 8,如果您的应用程序请求在后台使用位置的权限,则需要以下registry项(将string值设置为任何您喜欢的值):
<key>NSLocationAlwaysUsageDescription</key> <string>My app requires constant access to your location, even when the screen is off.</string>
如果您的应用程序仅在前台使用位置,请添加以下registry项:
<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
- 确保“cordova.js”文件存在于根文件夹中,只要将这个JS文件包含在您访问此位置的.html文件中即可解决此问题。 没有花色需要。 如果这个js文件丢失了,那么navigator.geolocation就会调用基于浏览器的HTML5对象,因此会产生奇怪的警报。