我一直在遇到同样的问题,并且在过去的两周内尝试过无数的解决方案在网上找到这个没有成功,任何帮助赞赏:)
我正在使用Cordova为IOS和Android编写一个简单的应用程序,并使用Phonegap构建它 . 问题是我无法让“navigator.geolocation.getCurrentPosition”(或navigator.geolocation.watchPosition)调用返回除iOS上的超时错误之外的任何内容 . (在Android上完美运行)
Additionally, the location services dialog never appears (requesting user permission to allow GPS for the app)
为了增加这种奇怪的行为,似乎我必须在加载应用程序后触摸屏幕以启动地理定位调用,在大多数情况下,应用程序只是坐在那里并且在屏幕被触摸之前什么都不做 .
使用iOS 9在iPhone 5上进行测试
我试过的事情:
-
更改超时设置(最长30秒)
-
设置enableHighAccuracy true / false
-
运行带或不带geolocation插件的代码
-
手动将NSLocationWhenInUseUsageDescription / NSLocationAlwaysUsageDescription设置(或两者)添加到plist文件
-
为地理定位插件安装不同的插件版本
-
更改内容安全策略元标记
-
尝试从root config.xml添加或删除
<plugin>
声明
Current plugins installed ($cordova plugin list):
cordova-plugin-dialogs 1.2.0 "Notification"
cordova-plugin-geolocation 1.0.1 "Geolocation"
cordova-plugin-whitelist 1.2.0 "Whitelist"
cordova.plugins.diagnostic 2.3.5 "Diagnostic"
Root config.xml:
<?xml version='1.0' encoding='utf-8'?>
<widget id="info.test" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<name>App name</name>
<description>
App Name
</description>
<author email="test@test.com" href="http://test.com">
App Team
</author>
<content src="index.html" />
<plugin name="cordova-plugin-whitelist" spec="1" />
<access origin="*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
<platform name="android">
<allow-intent href="market:*" />
</platform>
<platform name="ios">
<allow-intent href="itms:*" />
<allow-intent href="itms-apps:*" />
</platform>
</widget>
index.js
var geo = {
getGeo: function() {
navigator.geolocation.getCurrentPosition(
geo.onSuccess,
geo.onError,
{maximumAge:0, timeout: 5000, enableHighAccuracy: false});
},
onSuccess: function(position) {
alert('GOT location');
alert(position.coords.latitude + ' --- ' + position.coords.longitude);
},
onError: function(error) {
alert('error getting geo!');
}
};
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
app.receivedEvent('deviceready');
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
geo.getGeo();
}
};
app.initialize();
iOS Plist file:
<key>NSLocationAlwaysUsageDescription</key>
<string>This app requires constant access to your location in order to track your position, even when the screen is off.</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string/>
Index.html:
<html>
<head>
<!--
Customize this policy to fit your own app's needs. For more guidance, see:
https://github.com/apache/cordova-plugin-whitelist/blob/master/README.md#content-security-policy
Some notes:
* gap: is required only on iOS (when using UIWebView) and is needed for JS->native communication
* https://ssl.gstatic.com is required only on Android and is needed for TalkBack to function properly
* Disables use of inline scripts in order to mitigate risk of XSS vulnerabilities. To change this:
* Enable inline JS: add 'unsafe-inline' to default-src
-->
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<link rel="stylesheet" type="text/css" href="css/index.css">
<title>Hello World</title>
</head>
<body>
<div class="app">
<h1>Apache Cordova</h1>
<div id="deviceready" class="blink">
<p class="event listening">Connecting to Device</p>
<p class="event received">Device is Ready</p>
</div>
</div>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
</body>
</html>
任何正确方向的帮助表示赞赏!
1 回答
@daddio
哦 . 而已 . 我应该更加教条 . 这应该解决问题 .
不建议使用Phonegap CLI来制作Phonegap Build构建项目 . Phonegap构建要求
index.html
和config.xml
都位于根目录中 . 您需要与Phonegap CLI一起使用的所有额外文件永远不会被Phonegap Build创建或需要 .所以,我打算给你一个我的工作演示 . 请注意,所有文件都在一(1)个目录中 .
教程/博客文章 - An HTML Boilerplate for Phonegap Build
源代码Phonegap--Generic-Boilerplate
请注意编译器版本如何设置为:
<preference name="phonegap-version" value="cli-5.2.0" />
请注意,如果您将
cli-5.2.0
更改为3.7.0
,则似乎没有任何更改 .基本上对于Phonegap Build,你需要
index.html
和config.xml
- 就是这样 . 然后出于安全原因(从Cordova Tools 5.0.0开始),建议创建css
文件和javascript
文件 . 但是,我会告诉你如何解决这个问题 .这是我完整的演示应用列表 .
你的修复
这是我的工作代码,使用3.5.0和cli-5.1.1以及cli5.2.0进行测试
在您的修复程序上,请注意版本的设置方式:
<preference name="phonegap-version" value="3.5.0" />
好的,我相信所有这一切都适合你 . 因此,您的文档在这里:https://build.phonegap.com/docs
添加插件有一些特殊规则,但我会在程序运行时给你这些规则 .
如何使用Phonegap Build添加插件
对于最直接的未来,从这个列表中获取你的核心插件:http://cordova.apache.org/docs/en/5.4.0/cordova/plugins/pluginapis.html
从此来源获取第三方插件:http://cordova.apache.org/plugins/
使用Phonegap Build时,有时插件会被修复(或更新),这会破坏Phonegap Build . 这是因为"fixes"需要最新版本的编译器,而Phonegap Build总是至少有一个版本落后 .
有两种方法可以处理3;见4和5 .
我创建了this Worksheet . 您可能想要制作副本或仅将其用作参考 . 我使用这个工作表来创建我的演示,我知道列表是好的 . 我正在研究cli-5.2.0 . (应该在星期一左右结束 . )但是,我不测试所有第三方插件;有800个插件 .
如果您未能在插件上设置版本号,您将获得最新版本 . 如果您的构建失败,则设置版本 . 如果构建仍然失败,请尝试前两个或三个版本以找到有效的版本 . 注意,这些较旧的插件可能存在阻止您使用它们的错误 . 所以,尝试更早的版本 .
最后,如果你认为你遇到了一个bug,那么这里是a page with links to the Bug respository for each plugin . 最后更新位于左上角 . 最好的运气 .