返回 导航

Cordova

hangge.com

Cordova - 使用Cordova开发iOS应用实战3(添加Cordova控制台插件)

作者:hangge | 2016-04-19 08:40
前文介绍了通过 SafariWeb检查器,可以看到控制台输出的信息。但有时这样调试代码不太方便,如果在Xcode中的命令控制台也能同步打印出调试信息就好了。
这个借助Cordova的 cordova-plugin-console 插件即可实现。

1,给项目添加cordova-plugin-console插件
(1)在“终端”中进入项目文件夹
(2)输入如下命令:
cordova plugin add cordova-plugin-console

2,测试样例
我们将首页 index.html 修改成如下内容,测试各种类型的控制台信息。
<!DOCTYPE html>
<html>
    <head>
        <title>Pause Example</title>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8">
            <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
            <script type="text/javascript" charset="utf-8">
                document.addEventListener("deviceready", onDeviceReady, false);
                function consoleLog(){
                    console.log("console.log works well");
                }
                function consoleError(){
                    console.error("console.error works well");
                }
                function consoleException(){
                    console.exception("console.exception works well");
                }
                function consoleWarn(){
                    console.warn("console.warn works well");
                }
                function consoleInfo(){
                    console.info("console.info works well");
                }
                function consoleDebug(){
                    console.debug("console.debug works well");
                }
                function consoleAssert(){
                    console.assert("console.assert works well");
                }
                function consoleDir(){
                    console.dir("console.dir works well");
                }
                function consoleDirxml(){
                    console.dirxml("console.dirxml works well");
                }
                function consoleTime(){
                    console.time("console.time works well");
                }
                function consoleTimeEnd(){
                    console.timeEnd("console.timeEnd works well");
                }
                function consoleTable(){
                    console.table("console.table works well");
                }
            </script>
            <style type="text/css">
                button {
                    width: 200px;height:26px;font-size: 20px;padding: 1px;margin-left: 50px;
                }
            </style>
            </head>
        <body>
            <br/><button onclick="consoleLog()">consoleLog</button><br/>
            <br/><button onclick="consoleError()">consoleError</button><br/>
            <br/><button onclick="consoleException()">consoleException</button><br/>
            <br/><button onclick="consoleWarn()">consoleWarn</button><br/>
            <br/><button onclick="consoleInfo()">consoleInfo</button><br/>
            <br/> <button onclick="consoleDebug()">consoleDebug</button><br/>
            <br/><button onclick="consoleAssert()">consoleAssert</button><br/>
            <br/> <button onclick="consoleDir()">consoleDir</button><br/>
            <br/> <button onclick="consoleDirxml()">consoleDirxml</button><br/>
            <br/><button onclick="consoleTime()">consoleTime</button><br/>
            <br/><button onclick="consoleTimeEnd()">consoleTimeEnd</button><br/>
            <br/><button onclick="consoleTable()">consoleTable</button><br/>
    </body>
</html>
运行后页面如下:

3,测试结果
(1)把页面上的按钮都点一遍,看到Safari的Web检查器中打印出了各种控制台信息。


(2)同样在Xcode这边的控制台,同样有调试信息打印出来

评论

全部评论(1)

回到顶部