一、准备
插件功能:打开IOS相机
1:创建插件
plugman create --name [插件名称] --plugin_id [插件ID] --plugin_version [插件版本号]
plugman create --name SwiftPluginDemo --plugin_id cordova-plugin-swiftplugindemo --plugin_version 1.0.0
2:添加IOS平台
plugman platform add --platform_name ios
3:创建package.json文件
以下两种都可以生成package.json
1:使用命令 “npm init” 创建package.json文件
2:plugman createpackagejson [插件路径]
最终插件目录结构
SwiftPluginDemo.swift 是由 SwiftPluginDemo.m 修改了后缀名而来,也可以删除SwiftPluginDemo.m,新建SwiftPluginDemo.swift
二、过程
SwiftPluginDemo.swift
/********* SwiftPluginDemo.m Cordova Plugin Implementation *******/
// start OC 原代码
// #import <Cordova/CDV.h>
// @interface SwiftPluginDemo : CDVPlugin {
// // Member variables go here.
// }
// - (void)coolMethod:(CDVInvokedUrlCommand*)command;
// @end
// @implementation SwiftPluginDemo
// - (void)coolMethod:(CDVInvokedUrlCommand*)command
// {
// CDVPluginResult* pluginResult = nil;
// NSString* echo = [command.arguments objectAtIndex:0];
// if (echo != nil && [echo length] > 0) {
// pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:echo];
// } else {
// pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
// }
// [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
// }
// @end
// end OC 原代码
// swift 开始
//1 swift 被oc调用,必须使用 @objc 显示声明类和方法
//2 oc调用swfit中的方法,需要在方法名后面加WithCommand
import AVFoundation
@objc(SwiftPluginDemo) class SwiftPluginDemo : CDVPlugin{
// override func pluginInitialize(){
// NSLog("===========SwiftPluginDemo 初始化")
// super.pluginInitialize();
// }
@objc func verifyPassWord(command: CDVInvokedUrlCommand){
NSLog("====================== 验证密码")
// 返回结果
let pluginResult:CDVPluginResult?
//获取参数
let password = command.arguments[0] as? String
//开始验证密码
if password == nil || password == "" {
pluginResult = CDVPluginResult(status:CDVCommandStatus_ERROR,messageAs:"密码不能为空")
}else if password != "admin" {
pluginResult = CDVPluginResult(status:CDVCommandStatus_ERROR,messageAs:"密码错误")
}else{
pluginResult = CDVPluginResult(status:CDVCommandStatus_OK)
}
//将验证结果返回
self.commandDelegate.send(pluginResult, callbackId: command.callbackId);
}
}
SwiftPluginDemo.js
// start oc 原代码
// var exec = require('cordova/exec');
// exports.coolMethod = function (arg0, success, error) {
// exec(success, error, 'SwiftPluginDemo', 'coolMethod', [arg0]);
// };
// end oc 原代码
// start swift 代码
var exec = require('cordova/exec');
// exports.verifyPassWord = function (arg0, success, error) {
// console.log("=================== js 中的verifyPassWord被调用");
// exec(success, error, 'SwiftPluginDemo', 'verifyPassWordWithCommand', [arg0]); // 如果是swift 那么方法名后面加WithCommand,不然无法识别
// };
// exports.initialize = function (arg0, success, error) {
// exec(success, error, 'SwiftPluginDemo', 'initializeWithCommand', [arg0]);
// };
//OK
var SwiftPluginDemo = function() {};
SwiftPluginDemo.prototype.verifyPassWord = function (arg0, success, error) {
exec(success, error, 'SwiftPluginDemo', 'verifyPassWordWithCommand', [arg0]);
}
module.exports = new SwiftPluginDemo();
// OK
// module.exports = {
// verifyPassWord: function (arg0, success, error) {
// exec(success, error, 'SwiftPluginDemo', 'verifyPassWordWithCommand', [arg0]);
// }
// };
//module.exports 和 exports 是不一样的
//实际上调用返回的都是module.exports ,exports收集到的属性和方法,最总都会赋值给module.exports(前提是module.exports是没有任何属性和方法)
// hello.js
// funciton hello () {
// console.log('hello');
// }
// 1.
// export.hello = hello;
// var aaa = require('hello.js');
// aaa.hello(); // console.log('hello');
// 2.
// module.export = hello;
// var aaa = require('hello.js');
// aaa(); // console.log('hello');
plugin.xml
<?xml version='1.0' encoding='utf-8'?>
<plugin id="cordova-plugin-swiftplugindemo" version="1.0.0"
xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android">
<name>SwiftPluginDemo</name>
<js-module name="SwiftPluginDemo" src="www/SwiftPluginDemo.js">
<!--个人理解 clobbers 就是js-module中src这个js module.exports的对象。。或者可以认为就是js文件 -->
<!--var cordova.plugins.SwiftPluginDemo = require('www/SwiftPluginDemo.js') -->
<clobbers target="cordova.plugins.SwiftPluginDemo" />
</js-module>
<platform name="ios">
<config-file parent="/*" target="config.xml">
<feature name="SwiftPluginDemo">
<param name="ios-package" value="SwiftPluginDemo" />
<param name="onload" value="true" />
</feature>
</config-file>
<source-file src="src/ios/SwiftPluginDemo.swift" />
</platform>
</plugin>
三、最后
cordova 插件类本身应该是使用oc开发的,所以如果想要Cordova支持swift,那么就需要进行一些配置。配置的内容和oc使用swift差不多
1、桥接文件,一般是swift工程,在创建一个oc文件时,系统自动添加(不用改名,直接默认即可)
2、将swift需要引用的oc文件 .h头文件 添加到桥接类中。
如果你打开ionic cordova项目的桥接文件,会发现里面已经 #import <Cordova/CDV.h>
@objc(SwiftPluginDemo) class SwiftPluginDemo : CDVPlugin
CDVPlugin 就是由<Cordova/CDV.h>提供
本插件swift没有调用其他oc方法,所以桥接文件不需要添加其他的.h文件
3、在xcode设置swift版本