javascript 即使添加了控制台插件,console.log 也无法在 iOS Phonegap App 中运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21826277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 21:46:26  来源:igfitidea点击:

console.log is not working in iOS Phonegap App even after adding the console plugin

javascriptioscordovacordova-plugins

提问by Shashi

I have created a Phonegap iOS App in Cordova CLI . I have added the console plugin and yes deviceready is called successfully but console.log is not working and does not print any thing in XCode log.

我在 Cordova CLI 中创建了一个 Phonegap iOS 应用程序。我已经添加了控制台插件,是的 deviceready 被成功调用,但 console.log 不工作,也没有在 XCode 日志中打印任何内容。

Plugin Installation :-

插件安装:-

cordova -v
3.3.1-0.3.1

sudo cordova plugins add org.apache.cordova.console
Fetching plugin "org.apache.cordova.console" via plugin registry
Starting installation of "org.apache.cordova.console" for ios
Preparing ios project
org.apache.cordova.console installed on ios.

sudo cordova plugins ls
[ 'org.apache.cordova.console',
'org.apache.cordova.device',
'org.apache.cordova.dialogs',
'org.apache.cordova.geolocation',
'org.apache.cordova.globalization',
'org.apache.cordova.inappbrowser',
'org.apache.cordova.media',
'org.apache.cordova.network-information',
'org.apache.cordova.splashscreen',
'org.apache.cordova.vibration' ]

科尔多瓦-v
3.3.1-0.3.1

sudocordova 插件添加 org.apache.cordova.console
通过插件注册表获取插件“org.apache.cordova.console”
开始为 ios 安装“org.apache.cordova.console”
准备 ios 项目
org.apache.cordova.console 安装在ios上。

须藤科尔多瓦插件 ls
['org.apache.cordova.console'、
'org.apache.cordova.device'、
'org.apache.cordova.dialogs'、
'org.apache.cordova.geolocation'、
'org.apache. cordova.globalization'、
'org.apache.cordova.inappbrowser'、
'org.apache.cordova.media'、
'org.apache.cordova.network-information'、
'org.apache.cordova.splashscreen'、
'org. apache.cordova.vibration']

Java Script :-

Java脚本:-

var app = {
    initialize: function() {
       this.bindEvents();
    },
    bindEvents: function() {
       document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    onDeviceReady: function() {
       app.receivedEvent('deviceready');
    },
    receivedEvent: function(id) {
       console.log('Device Ready Received'); //It is not working
       alert("Device ready called");  //It is Working
    }
};

采纳答案by Shashi

I have solved this issue by doing these steps

我已经通过执行这些步骤解决了这个问题

Step 1 :

步骤1 :

I Just copy a directory from another cordova project where console.log was working

我只是从console.log正在工作的另一个cordova项目中复制一个目录

sudo cp -r DIFF_CORDOVA_PROJECT_PATH/platforms/ios/www/plugins/org.apache.cordova.console CURRENT_CORDOVA_PROJECT_PATH/platforms/ios/www/plugins/

Step 2 :

第2步 :

Add the Code in CURRENT_CORDOVA_PROJECT_PATH/platforms/ios/www/cordova_plugins.js file under module.exports JSON array

将代码添加到 CURRENT_CORDOVA_PROJECT_PATH/platforms/ios/www/cordova_plugins.js 文件中 module.exports JSON 数组下

{
    "file": "plugins/org.apache.cordova.console/www/console-via-logger.js",
    "id": "org.apache.cordova.console.console",
    "clobbers": [
        "console"
    ]
},
{
    "file": "plugins/org.apache.cordova.console/www/logger.js",
    "id": "org.apache.cordova.console.logger",
    "clobbers": [
        "cordova.logger"
    ]
}

Step 3 :

第 3 步:

Adding Meta data on the same cordova_plugins.js file in module.exports.metadata JSON Array :-

在 module.exports.metadata JSON Array 中的同一个cordova_plugins.js 文件上添加元数据:-

"org.apache.cordova.console": "0.2.7"

“org.apache.cordova.console”:“0.2.7”

回答by Joe Aspara

In my case the reason was probably because the plugin was installed from a Windows machine the first time and it work previously only for the Android platform. when I try to reinstall it from my Macintosh I encountered the same problem of Shashi.

在我的情况下,原因可能是因为该插件是第一次从 Windows 机器上安装的,它以前仅适用于 Android 平台。当我尝试从 Macintosh 重新安装它时,我遇到了与 Shashi 相同的问题。

This has been caused from the missing of the source file CDVLogger.m in the compile source list. So I recommend to check if this file is already added by:

这是由于编译源列表中缺少源文件 CDVLogger.m 造成的。因此,我建议检查此文件是否已通过以下方式添加:

Click on root Project file in the Xcode explorer > Build Phases > Compile Sources section

单击 Xcode 资源管理器中的根项目文件 > 构建阶段 > 编译源部分

If the CDVLogger.m file is not present add it and try to Run the project again. This fixed the issue for me.

如果 CDVLogger.m 文件不存在,请添加它并再次尝试运行该项目。这为我解决了这个问题。

EDIT: In addition be sure that your index.html page contains this HTML:

编辑:另外请确保您的 index.html 页面包含此 HTML:

<div id="deviceready" class="blink">
    <p class="event listening">Connecting to Device</p>
    <p class="event received">Device is Ready</p>
</div>

回答by mboeckle

I tried a lot of solutions posted here in stackoverflow but nothing was working for me. After putting the cordova.js outside of my personal JS folder console.log was working fine.

我尝试了很多在 stackoverflow 中发布的解决方案,但没有任何效果对我有用。将cordova.js 放在我的个人JS 文件夹console.log 之外后工作正常。

<script charset="utf-8" src="cordova.js"></script>

回答by BasssS

add <script type="text/javascript" src="cordova.js"></script>inside the section of the code

<script type="text/javascript" src="cordova.js"></script>在代码部分内添加

回答by Chris D. Emerson

I've had this as well. I would get no console output and my app's default.html file would never really load. The issue in my situation was that my console plugin was literally one decimal off/old (0.2.12 instead of 0.2.13). Soon as I updated everything started working again... argh!

我也有过这种情况。我不会得到任何控制台输出,我的应用程序的 default.html 文件永远不会真正加载。我的情况的问题是我的控制台插件实际上是小数点/旧(0.2.12 而不是 0.2.13)。我更新后一切又开始工作了......啊!

Related: https://stackoverflow.com/a/32035260/826308

相关:https: //stackoverflow.com/a/32035260/826308

回答by Paradise

Make sure you do following action:

确保您执行以下操作:

function onDeviceReady() {
if (window.cordova.logger) {
    window.cordova.logger.__onDeviceReady();
} } document.addEventListener('deviceready', onDeviceReady, false);`

回答by Baljeet Singh

In my case, following div was missing

就我而言,缺少以下 div

<div id="deviceready" class="blink">
  <p class="event listening">Connecting to Device</p>
  <p class="event received">Device is Ready</p>
</div>