繁体   English   中英

iOS超时时的Cordova / Phonegap构建地理位置

[英]Cordova/Phonegap Build geolocation on iOS timeout

我一直遇到同样的问题,并且在过去的2周中尝试了许多在线解决方案,但均未成功,不胜感激:)

我正在使用Cordova(iOS和Android)编写一个简单的应用程序,并使用Phonegap构建它。 问题是我无法获取“ navigator.geolocation.getCurrentPosition”(或navigator.geolocation.watchPosition)调用以返回除iOS上的超时错误以外的任何内容。 (在Android上完美运行)

此外,永远不会出现位置服务对话框(请求用户许可以允许该应用使用GPS)

为了增加这种奇怪的行为,似乎我必须在加载该应用程序后触摸屏幕以发起地理定位调用,在大多数情况下,该应用程序只是坐在那里,在触摸屏幕之前什么也不做。

在装有iOS 9的iPhone 5上进行测试

我尝试过的事情:

  1. 更改超时设置(最长30秒)
  2. 设置enableHighAccuracy true / false
  3. 使用或不使用地理位置插件运行代码
  4. 手动将NSLocationWhenInUseUsageDescription / NSLocationAlwaysUsageDescription设置(或两者)添加到plist文件中
  5. 为地理位置插件安装不同的插件版本
  6. 更改内容安全策略元标记
  7. 尝试从根config.xml添加或删除<plugin>声明

当前安装的插件($ cordova插件列表):

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"

根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文件:

    <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>

朝正确方向的任何帮助表示赞赏!

@daddio
哦。 而已。 我对此应该更加教条。 这应该解决问题。

建议不要使用Phonegap CLI创建Phonegap Build构建项目。 Phonegap构建要求index.htmlconfig.xml都位于根目录中。 Phonegap CLI一起使用所需的所有其他文件,都不会由Phonegap Build创建或使用。

因此,我将给您一个我的工作演示。 请注意,所有文件都在一(1)个目录中。

请注意,编译器版本设置为:

<preference name="phonegap-version" value="cli-5.2.0" />

注意,如果将cli-5.2.0更改为3.7.0 ,则似乎没有任何变化。

基本上,对于Phonegap Build ,您需要index.htmlconfig.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添加插件

  1. 对于最近期的事情,请从以下列表中获取* core *插件: http : //cordova.apache.org/docs/en/5.4.0/cordova/plugins/pluginapis.html

  2. 从此来源获取您的第三方插件: http : //cordova.apache.org/plugins/

  3. 使用Phonegap Build时 ,有时插件会被固定(或更新),这会使Phonegap Build中断。 这是因为“修复程序”需要编译器的最新版本,而Phonegap Build始终至少落后一个版本。

处理3有两种方法: 参见4和5。

  1. 我已经创建了此工作表 您可能需要制作副本或仅将其用作参考。 我使用此工作表创建演示,所以我知道列表很好。 我正在研究cli-5.2.0。 (应该在星期一左右完成。)但是,我不测试所有第3方插件。 有800多个插件。

  2. 如果您无法在插件上设置版本号,则将获取最新版本。 如果构建失败,则设置版本。 如果构建仍然失败,请尝试使用先前的两个或三个版本来找到可用的版本。 注意,这些较旧的插件可能存在阻止您使用它们的错误。 因此,请尝试使用更早的版本。

最后,如果您认为自己遇到了错误,那么这是一个页面,其中包含每个插件的错误存储库的链接 最后更新位于左上方。 祝你好运。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM