Cordova and Ios

1.cordova_plugins.js

..........

Model 是调用插件的时候的前缀 比如Model.coolMethod(alertSuccess,alertFail,["123"]),Model名字随意去起名字
"clobbers": [
        "Model"
      ]

2.html + js + css写到一个文件里面了

<!DOCTYPE html>
<html>
    <head>
        <title>AMAlert</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">

            function testPlugin() {
                Model.coolMethod(alertSuccess,alertFail,["q2qqw"])
            }

            function alertSuccess(msg) {
                alert(msg);
            }

            function alertFail(msg) {
                alert('error: ' + msg);
            }
            </script>
    </head>

    <body style="padding-top:50px">
        <button style="font-size:17px;" onclick="testPlugin();">调用iOS插件</button> <br>
    </body>
</html>

也可以使用匿名函数来调用

function testPlugin() {
                Model.coolMethod("1000",function(msg){
                    alert(msg);
                },function(msg){
                    alert('error: ' + msg);
                })
            }

3.OC类名.js

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

exports.coolMethod = function (arg0, success, error) {
    exec(success, error, 'OC类名', 'coolMethod', [arg0]);
};

});

4.打包插件

com.......jsplugin/src/ios
存储插件内容 并且配置plugin.xml文件
使用命令添加插件即可

5.升级报错

sudo npm install npm@latest -g
sudo npm update -g cordova

升级报错 进入cordova项目
npm WARN registry Unexpected warning for http://registry.cnpmjs.org/

rm ./package-lock.json


rm -r ./node_modules


npm cache clear --force
或许可以解决
posted @ 2020-05-08 18:19  yxg889  阅读(137)  评论(0编辑  收藏  举报