代码改变世界

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

2016-04-19 15:55 by 笨小宝, ... 阅读, ... 评论, 收藏, 编辑

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

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

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

2,测试样例
我们将首页 index.html 修改成如下内容,测试各种类型的控制台信息。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<!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>
运行后页面如下:
原文:Cordova - 使用Cordova开发iOS应用实战3(添加Cordova控制台插件)

3,测试结果
(1)把页面上的按钮都点一遍,看到Safari的Web检查器中打印出了各种控制台信息。
原文:Cordova - 使用Cordova开发iOS应用实战3(添加Cordova控制台插件)


(2)同样在Xcode这边的控制台,同样有调试信息打印出来
原文:Cordova - 使用Cordova开发iOS应用实战3(添加Cordova控制台插件)

原文出自:www.hangge.com  转载请保留原文链接:http://www.hangge.com/blog/cache/detail_1139.html