码迷,mamicode.com
首页 > 移动开发 > 详细

ionic cordova plugin for ios

时间:2015-07-10 23:31:46      阅读:306      评论:0      收藏:0      [点我收藏+]

标签:

源代码结构目录:

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;

 

 

payplugin\plugin.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>

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

payplugin\src\ios\CDVPayPlugin.h

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

@interface CDVPayPlugin : CDVPlugin

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

@end

 

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

payplugin\src\ios\CDVPayPlugin.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

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

payplugin\src\android\PayPlugin.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‘);
}
})

 

ionic cordova plugin for ios

标签:

原文地址:http://www.cnblogs.com/yuanxiaoping_21cn_com/p/4637655.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!