如果关闭位置服务,Phonegap构建“deviceready”事件永远不会触发

时间:2013-06-03 16:07:14

标签: iphone jquery-mobile phonegap-build

我已经使用JQM(1.3.1),jQuery(1.9),CSS3和javascript编写了一个webapp现在我处于第二阶段,我正在使用手机间隙(2.7)将此应用程序移植到Apple商店(目标iOS( 6.1)/ iPhone5 - 可以在iPhone 4S / iOS 6.0上重现问题

如下所示,我的地理定位代码在浏览器中执行时效果很好。

var geoOptions = { 'enableHighAccuracy': true, 'timeout': 10000, 'maximumAge': 0 };
navigator.geolocation.getCurrentPosition(geoSuccess, geoError, geoOptions);

function geoSuccess(postion)
{
  //on success code here    
}
function geoError(error)
{
  //on error code here    
}

然而,一旦通过phonegap,我的应用程序请求用户两次访问位置服务的权限,即它显示弹出两次。因此,为了解决这个问题,我在stackoverflow上的许多解决方案中建议使用下面的代码。

function onDeviceReady() {
 navigator.geolocation.getCurrentPosition(geoSuccess, geoError,geoOptions);
}

$(function() {
 document.addEventListener("deviceready", onDeviceReady, false);
});

这解决了弹出问题,但开始了新的问题。

问题:如果位置服务被禁用,我的代码会添加deviceready listener并等待deviceready事件触发,但事件永远不会触发,它就在那里。如果位置服务已开启,则按预期工作。

有没有人遇到过这个问题,你能不能告诉我你为解决这个问题做了什么,请为我投入一天的时间来调试和研究时间。

亲切的问候,谢谢你。

1 个答案:

答案 0 :(得分:2)

问题解决了! :)

原来我没有完全按照phonegap构建文章中的描述包含手机间隙库。

查看此文章https://build.phonegap.com/docs/preparing-your-app

你不需要在每个页面上都包含这个库,只需要在索引页面中包含这个库,我不确定它是如何产生任何区别的,但是当我删除对任何其他的phonegap.js的任何引用后,“deviceready”事件开始触发页面,但index.html。

我注意到的第二件事是,由于某些原因,我的错误处理如下所示在phonegap构建之后停止工作。

switch(error.code) 
{
  case error.PERMISSION_DENIED:
   search.openPanelForSearch("Either the app was denied permission or the location srvice is currently turned off.", showInitialMap);
   break;
  case error.POSITION_UNAVAILABLE:
   search.openPanelForSearch("Geolocation information was unavailable. Would you like to try out a manual serach instead?", showInitialMap);
   break;
  case error.TIMEOUT:
   search.openPanelForSearch("Service was timed out since it took too long to retrieve the gelolcations. Would you like to try out a manual search instead?", showInitialMap);
   break;
  case error.UNKNOWN_ERROR:
   search.openPanelForSearch("Sorry an unknown error occurred. Would you like to try out a manual serach instead?", showInitialMap);
   break;
}

我不得不将其改为

switch(error.code) 
{
  case 1:
   search.openPanelForSearch("Either the app was denied permission or the location service is currently turned off.", showInitialMap);
   break;
  case 2:
   search.openPanelForSearch("Geolocation information was unavailable. Would you like to try out a manual search instead?", showInitialMap);
   break;
  case 3:
   search.openPanelForSearch("Service was timed out since it took too long to retrieve the gelolcations. Would you like to try out a manual serach instead?", showInitialMap);
   break;
  default:
   search.openPanelForSearch("Sorry an unknown error occurred. Would you like to try out a manual search instead?", showInitialMap);
   break;
}

希望这有助于某人。感谢。