Cordova 2.2.0在iOS上 - RequireJS不会正确加载Cordova [英] Cordova 2.2.0 on iOS - RequireJS won't load Cordova correctly

查看:721
本文介绍了Cordova 2.2.0在iOS上 - RequireJS不会正确加载Cordova的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Cordova(Phonegap),Backbone和JQtouch开发一个webapp。
其他事项,我需要在用户日历中添加事件。



在Android上一切正常。我还在使用Cordova 2.0.0。 (我没有升级到最新版本)。滚动工作,导航就OK,我可以在我的日历中添加活动!



在iPhone上,它是不同的。因为我想我的应用程序在iOS 6上工作,我在我的Mac上的Cordova 2.2.0。从那以后,我不能再在日历中添加事件了。
它在cordova 2.0.0工作(在iphone上),但现在不行。



调查后,我发现 cordova.exec



我搜索了很多关于这个问题,但似乎没有人,除了我,现在遇到了这个问题。



这里是使用Cordova 2.0.0但不使用Cordova 2.2.0的代码示例:



calendar.js,日历Cordova插件。我没有写。在Android上,我收到消息cordova.exec被定义,而在iOS上我得到另一个。

  // Cordova日历插件
//作者:Felix Montanez
//创建时间:01-17-2012
//贡献者:
// Michael Brooks

function calendarPlugin ()
{
}

calendarPlugin.prototype.createEvent = function(title,location,notes,startDate,endDate)
{
if function'== typeof(cordova.exec)){
alert(cordova.exec is defined);
} else {
alert(cordova.exec is not defined);
}
cordova.exec(null,null,calendarPlugin,createEvent,[title,location,notes,startDate,endDate]);
};

calendarPlugin.install = function()
{
if(!window.plugins)
{
window.plugins = {};
}
window.plugins.calendarPlugin = new calendarPlugin();
return window.plugins.calendarPlugin;
};

cordova.addConstructor(calendarPlugin.install);

调用函数createEvent的代码(它正在工作,因为我有上一个警报)

  if(confirm('Do you want to add this event in your calendar?'))
{
calendarPlugin .prototype.createEvent('<%= paramEvent_map%>','Geneva',
'Convocation','<%= paramEvent_startDate%>','<%= paramEvent_endDate%> ;
}

这个问题的一个可能的原因可能是我从Cordova 2.0升级的方式。 0到Cordova 2.2.0:我刚刚按照教程将Cordova 2.1.0项目升级到2.2。 0 。我应该是从2.0.0到2.1.0,然后表单2.1.0到2.2.0?



我会感激任何建议,因为我真的不想重新启动我的安装phonegap。



在Mac上,我使用Mountain Lion 10.8和xCode 4.5, m在iOS 4和6上测试我的应用程序。
在PC上,我正在使用Aptana studio 3和Eclipse 3.7.1,我正在Android 2.3上测试。



--- 编辑:重新安装Cordova 2.1.0,然后升级到2.2.0 ---



我刚刚删除了我的项目,卸载Cordova,并重做一切从头开始:





然后我在我的iPhone 3(iOS 6)上编译并启动我的项目, cordova.exec仍然未定义



--- EDIT:将我的日历插件声明为一个模块 ---



对于日历插件直接在cordova-2.2.0.js(我绝望)。



现在在cordova-2.2.0.js,我有以下行:(我从这里得到他们

  define(cordova / plugin / calendarplugin,function(require,exports,module){
var exec = require('cordova / exec');

var calendarPlugin = function(){};

calendarPlugin.prototype.createEvent = function(title,location,notes,startDate,endDate){
exec(null,null,'calendarPlugin','createEvent',[title,location, notes,startDate,endDate]);
}

var myCalendarPlugin = new calendarPlugin();
module.exports = myCalendarPlugin;
});

所以我不再使用cordova.exec

  var exec = require('cordova / exec'); 

我的日历插件文件现在只包含以下行:

  var mycalendarplugin = cordova.require(cordova / plugin / calendarplugin); 

这是我如何使用我的新模块:

  window.mycalendarplugin.createEvent('<%= paramEvent_map%>','Geneva',
'Convocation','<% = paramEvent_startDate%>','<%= paramEvent_endDate%>');

令人惊讶的是,函数exec()被调用!



但是,我收到以下消息:错误:尝试在'deviceready'之前调用cordova.exec()。忽略。当deviceReady



很遗憾,此事件永远不会触发。



--- 编辑:与Android比较 ---



我添加了几行来在Android和iOS上收听事件:

  window.addEventListener('load',function(){
alert(load triggered);
document.addEventListener('deviceready',function(){
alert(PhoneGap is now loaded!);
},false);
},false);



在iOS上,我收到消息加载触发,但不是PhoneGap现在加载。之后,我仍然不能使用exec()。



在Android上,我根本没有收到任何消息。但我可以使用cordova.exec()没有问题。



--- 编辑:从头重做项目 p>

而不是用cordova 2.1.0创建我的项目,然后升级到cordova 2.2.0,我试图直接使用cordova 2.2.0创建一个示例项目,然后包括日历插件, in(原始版本)中。



它工作得很好!有了更多的代码iOS 6(需要从用户明确授权),我可以添加事件在我的日历。



但是,一旦我添加我的项目的其余部分(html,css,js文件),我得到相同的错误:cordova.exec未定义



负责可以 RequireJS ,这可能以不同的方式加载cordova-2.2.0.js。它与cordova 2.0.0很好,但似乎不是与2.2.0。



我会尝试看看我是否可以加载cordova-2.2.0.js之前



我会告诉你最新的:)

解决方案

很抱歉回答我自己的问题。



RequireJS正在扰乱Cordova 2.2.0



之前,我使用此代码加载cordova:

  require.config({
paths:{
cordova:'libs / cordova / cordova-2.2.0',...

在我使用cordova的任何脚本之前,我写的是:

  define([
'jquery',
'cordova',
...
] {...}



在我的index.html中,我有:

 < script data-main =js / mainsrc =js / libs / require / require-jquery.js>< / script> ; 

它在cordova 2.0.0中运行良好!



要解决我遇到的问题




    我已经删除了前一行中关于cordova的所有内容。 .config。

  • 在我的js函数的定义部分
    中不再有cordova。



相反,我在我的index.html中只添加了一行:

 < script type =text / javascriptsrc = libs / cordova / cordova-2.2.0.js>< / script> 
< script data-main =js / mainsrc =js / libs / require / require-jquery.js>< / script&

一切正常!我可以再次呼叫cordova.exec()! (在iOS 4,iOS 6和iPhone 5上测试)



老实说,的这项工作。我只是假设cordova需要在其他任何东西(如jquery)之前加载,而RequireJS不善于这样做(或我不知道如何使用它)。



这是很糟糕的。我很高兴结束了:)



无论如何,我希望这对某人有用。


I'm currently developing a webapp using Cordova (Phonegap), Backbone and JQtouch. Among other things, I need to add events in the user calendar.

Everything works fine on Android. I'm still using Cordova 2.0.0. (I didn't upgrade to the latest version). The scrolling works, navigation is OK, and I can add events in my calendar!

On iPhone, it is different. As I want my app to work on iOS 6, I got Cordova 2.2.0 on my Mac. Since then, I can't add events in the calendar anymore. It worked (on iphone) with cordova 2.0.0, but not now.

After investigation, I found out that cordova.exec() is undefined.

I searched a lot about this issue but it seems nobody, except me, met that problem for now.

Here are samples of code that are working with Cordova 2.0.0 but not with Cordova 2.2.0:

calendar.js, a calendar plug-in for Cordova. I did not write it. On Android I get the message "cordova.exec is defined" while on iOS I get the other one.

// Cordova Calendar Plugin
// Author: Felix Montanez 
// Created: 01-17-2012
// Contributors:
// Michael Brooks    

function calendarPlugin()
{
}

calendarPlugin.prototype.createEvent = function(title,location,notes,startDate,endDate)
{
    if('function' == typeof(cordova.exec)) {
        alert("cordova.exec is defined");
    } else {
        alert("cordova.exec is not defined");
    }
    cordova.exec(null,null,"calendarPlugin","createEvent", [title,location,notes,startDate,endDate]);
};

calendarPlugin.install = function()
{
    if(!window.plugins)
    {
        window.plugins = {};
    }    
    window.plugins.calendarPlugin = new calendarPlugin();
    return window.plugins.calendarPlugin;
};

cordova.addConstructor(calendarPlugin.install);

The code which call the function createEvent (it's working, as I got the previous alert)

if (confirm('Do you want to add this event in your calendar?')) 
{ 
    calendarPlugin.prototype.createEvent('<%= paramEvent_map %>', 'Geneva', 
        'Convocation', '<%= paramEvent_startDate %>', '<%= paramEvent_endDate %>'); 
}

A possible source of this problem may be the way I upgraded from Cordova 2.0.0 to Cordova 2.2.0: I just followed the tutorial "Upgrading Cordova 2.1.0 projects to 2.2.0". Should I have done "From 2.0.0 to 2.1.0" then "Form 2.1.0 to 2.2.0"?

I'd appreciate any suggestion about this, as I really don't want to restart my installation of phonegap.

On Mac, I'm working with Mountain Lion 10.8 and xCode 4.5, and I'm testing my app on iOS 4 and 6. On PC, I'm working with Aptana studio 3 and Eclipse 3.7.1, and I'm testing on Android 2.3.

--- EDIT: Reinstall Cordova 2.1.0 then upgrade to 2.2.0 ---

I just erased my project, uninstall Cordova, and redo everything "from scratch":

  • I installed Cordova 2.1.0 from their website.
  • I upgraded xcode to 4.5.2 (as recommended)
  • I created a xcode project in which I copied parts of my code.
  • I followed their tutorial to upgrade from Cordova 2.1.0 to Cordova 2.2.0
  • I set "Architectures" to "armv7, armv7s" and "Build Active Architecture Only" to "Yes"
  • I added the frameworks I needed for the calendar plug-in: EventKit and EventKitUI

Then I compiled and launch my project on my iPhone 3 (with iOS 6), and cordova.exec is still not defined!

--- EDIT: Declare my calendar plugin as a module ---

I added some code for the calendar plug-in directly in cordova-2.2.0.js (I'm desperate).

Now in cordova-2.2.0.js, I have the following lines: (I got them from here)

define("cordova/plugin/calendarplugin", function(require, exports, module) {
    var exec = require('cordova/exec');

    var calendarPlugin = function() {};

    calendarPlugin.prototype.createEvent = function(title,location,notes,startDate,endDate) {
         exec(null, null, 'calendarPlugin', 'createEvent', [title,location,notes,startDate,endDate]);
    }

    var myCalendarPlugin = new calendarPlugin();
    module.exports = myCalendarPlugin;
});

So I don't use cordova.exec() anymore, but this instead:

var exec = require('cordova/exec');

My calendar "plug-in" file now just contains the following line:

var mycalendarplugin = cordova.require("cordova/plugin/calendarplugin");

And this is how I'm using my new "module":

window.mycalendarplugin.createEvent('<%= paramEvent_map %>', 'Geneva', 
'Convocation', '<%= paramEvent_startDate %>', '<%= paramEvent_endDate %>');

And, surprisingly, the function exec() is called!

However, I got the following message: "ERROR: Attempting to call cordova.exec() before 'deviceready'. Ignoring." which should appears when "deviceReady" has not fired.

Sadly, this event NEVER fires. So my problem is slightly different now, but still remains.

--- EDIT: Comparison with Android ---

I added a few lines to listen to events, both in Android and iOS:

window.addEventListener('load', function () {
     alert("load triggered");
     document.addEventListener('deviceready', function () {
            alert("PhoneGap is now loaded!");
     }, false);
}, false);

On iOS, I get the message "load triggered" but not "PhoneGap is now loaded". After that, I still can't use exec().

On Android, I get no message at all. But I can use cordova.exec() with no problem.

--- EDIT: Redo the project from scratch ---

Instead of creating my project with cordova 2.1.0 then upgrading to cordova 2.2.0, I tried to create a sample project directly with cordova 2.2.0 then include the calendar plug-in (original version) in it.

And it works perfectly fine! With a little more code for iOS 6 (which needs an explicit autorisation from the user), I can add events in my calendar.

However, as soon as I add the rest of my project (html, css, js files), I get the same error: cordova.exec is undefined.

The responsible could be RequireJS, which may load cordova-2.2.0.js in a different way. It worked well with cordova 2.0.0 but seems not to with 2.2.0.

I'll try to see if I can load cordova-2.2.0.js before RequireJS, and still use it once cordova is loaded.

I'll keep you up to date :)

解决方案

Sorry to answer my own question.

It was what I thought in my last edit: RequireJS is messing with Cordova 2.2.0!

Before, I used this code to load cordova:

require.config({
  paths: {
    cordova: 'libs/cordova/cordova-2.2.0', ...

Before any of my script which use cordova, I was writing:

define([
  'jquery',
  'cordova',
  ...
], function($) { ... }

In my index.html, I had:

<script data-main="js/main" src="js/libs/require/require-jquery.js"></script>

And it worked well with cordova 2.0.0! But with cordova 2.2.0, this is just WRONG.

To resolve my problem:

I got rid of everything about cordova in the previous lines.

  • No more cordova in require.config.
  • No more cordova in the define part of my js functions.

Instead, I added just one line in my index.html:

<script type="text/javascript" src="libs/cordova/cordova-2.2.0.js"></script>
<script data-main="js/main" src="js/libs/require/require-jquery.js"></script>

And everything works fine! I can call cordova.exec() again! (Tested on iOS 4, iOS 6, and on iPhone 5).

To be honest, I don't understand very well how all of this work. I just suppose that cordova needs to be loaded before everything else (like jquery), and RequireJS is not good at doing this (or I don't know how to use it).

It was awful going through this. I'm glad it's over :)

Anyway, I hope this will be useful for someone.

这篇关于Cordova 2.2.0在iOS上 - RequireJS不会正确加载Cordova的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆