• ionic cordova plugin for ios


    源代码结构目录:

    payplugin:

    |_src

      |_android

        |_PayPlugin.java

      |_ios

        |_CDVPayPlugin.h

        |_CDVPayPlugin.m

    |_www

      |_payplugin.js

    plugin.xml

    --======================================

    payplugin/www/payplugin.js

    var exec = require('cordova/exec');
    /*
    window.payplugin = function(str, callback) {
    cordova.exec(callback, function(err) {
    callback('Nothing to echo.');
    }, "jsnative", "echo", [str]);
    };
    //var o = new window.echo();
    module.exports = window.payplugin;*/


    /*
    var HelloWorld = function() {};

    HelloWorld.prototype.say = function() {
    alert("Hello World");
    };

    var helloWorld = new HelloWorld();
    module.exports = helloWorld;
    */

    /*window.payplugin = {};
    window.payplugin.add = function(args,args2,addSuc,addFaild) {
    cordova.exec(addSuc, addFaild, "payplugin", "add", [args,args2]);
    }
    module.exports = window.payplugin;*/

    var PayPlugin = function() {};

    PayPlugin.prototype.pay = function(args,args2,addSuc,addFaild) {
    cordova.exec(addSuc, addFaild, "payplugin", "pay", [args,args2]);
    }

    var payPlugin = new PayPlugin();
    module.exports = payPlugin;

    paypluginplugin.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
    id="com.youpin.payplugin"
    version="1.0.0">
    <name>payplugin</name>
    <description>Pay Plugin</description>
    <license>Apache 2.0</license>
    <keywords>payplugin</keywords>

    <js-module src="www/payplugin.js" name="payplugin">
    <clobbers target="PayPlugin" />
    </js-module>

    <!-- ios -->
    <platform name="ios">
    <config-file target="config.xml" parent="/*">
    <feature name="payplugin">
    <!-- CDVPayPlugin是类名 -->
    <param name="ios-package" value="CDVPayPlugin"/>
    </feature>
    </config-file>

    <header-file src="src/ios/CDVPayPlugin.h" />
    <source-file src="src/ios/CDVPayPlugin.m" />
    </platform>

    <!-- android -->
    <platform name="android">
    <config-file target="res/xml/config.xml" parent="/*">
    <feature name="payplugin" >
    <param name="android-package" value="com.youpin.payplugin"/>
    </feature>
    </config-file>

    <source-file src="src/android/PayPlugin.java" target-dir="src/com/youpin/payplugin" />
    </platform>
    </plugin>

    --=======================================

    paypluginsrciosCDVPayPlugin.h

    #import <UIKit/UIKit.h>
    #import <Cordova/CDVPlugin.h>
    #import <Cordova/CDV.h>

    @interface CDVPayPlugin : CDVPlugin

    - (void) pay:(CDVInvokedUrlCommand*)command;

    @end

    --=======================================

    paypluginsrciosCDVPayPlugin.m

    #import "CDVPayPlugin.h"
    #import <Cordova/CDV.h>

    @implementation CDVPayPlugin

    - (void) pay:(CDVInvokedUrlCommand*)command{
    CDVPluginResult* pluginResult = nil;
    NSString* javaScript = nil;

    @try {

    NSString* prod = [command.arguments objectAtIndex:0];
    NSString* money = [command.arguments objectAtIndex:1];

    if (prod != nil
    && [prod length] > 0
    && money != nil
    && [money length] > 0) {

    /**
    *相加字符串
    */
    NSString *addResult = [NSString stringWithFormat:@"%@%@", prod, money];
    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:addResult];
    javaScript = [pluginResult toSuccessCallbackString:command.callbackId];

    } else {
    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
    javaScript = [pluginResult toErrorCallbackString:command.callbackId];
    }
    } @catch (NSException* exception) {
    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_JSON_EXCEPTION messageAsString:[exception reason]];
    javaScript = [pluginResult toErrorCallbackString:command.callbackId];
    }

    [self writeJavascript:javaScript];
    }

    @end

    --============================================

    paypluginsrcandroidPayPlugin.java

    package com.youpin;

    import org.apache.cordova.CordovaPlugin;
    import android.content.Context;
    import android.util.Log;

    public class PayPlugin extends CordovaPlugin {
    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    if (action.equals("pay")) {
    String arg1 = args.getString(0);
    String arg2 = args.getString(1);
    String message = "result is:" + arg1 + arg2;
    this.echo(message, callbackContext);
    return true;
    }
    return false;
    }
    }

    安装方法

    ionic plugin add payplugin

    ionic build ios

    uninstall:

    ionic plugin remove com.youpin.payplugin

    调用方法:

    .controller('ChatDetailCtrl', function($scope, $stateParams, Chats) {

    $scope.$on('$ionicView.enter', function(e) {

    /*navigator.jsnative("echome", function(echoValue) {
    alert(echoValue); // should alert true.
    });*/

    PayPlugin.pay("youbi6","6.00",$scope.addSuc,$scope.addFiald);
    });


    /** 
    * js回调函数 
    */ 
    $scope.addSuc = function(result) {
      alert(result);
      console.log('addSuc='+result);
    }
    $scope.addFiald = function() {
      console.log('addFiald');
    }
    })

  • 相关阅读:
    JS组件系列——使用HTML标签的data属性初始化JS组件
    JS组件系列——分享自己封装的Bootstrap树形组件:jqTree
    JS组件系列——封装自己的JS组件,你也可以
    JS组件系列——Bootstrap 树控件使用经验分享
    JS组件系列——Bootstrap Select2组件使用小结
    面试干货——年底干货大放送,你准备好了吗?
    JS组件系列——基于Bootstrap Ace模板的菜单和Tab页效果分享(你值得拥有)
    JS组件系列——Form表单验证神器: BootstrapValidator
    HTTPS的加密过程
    Http知识总结
  • 原文地址:https://www.cnblogs.com/yuanxiaoping_21cn_com/p/4637655.html
Copyright © 2020-2023  润新知