iOS phoneGap的使用(二、自定义phoneGap插件)

来源:互联网 发布:网络彩票开售解禁通知 编辑:程序博客网 时间:2024/04/30 16:59

首先说明,本人实在创建phoneGap工程之后,添加了”device” 、”dialogs”插件之后,在platform/iOS目录下进行的<插件的自定义>的实现,其它无关都删除了;

如下图:
这里写图片描述

其中的”CordovaLib”也可以用静态库替代;
下面开始说明如何制作插件:

一、按如下图添加一个插件目录,我一hello为例:
这里写图片描述

直接从添加的其它目录copy一份,改下文件夹名字;修改里面的js文件,重命名”hello.js”,修改hello.js的内容(它的内容不能为空,也不能写错,不然会有bug的);
hello.js的空白格式可以按如下填写:

//hello.jscordova.define("cordova-plugin-hello.hello", function(require, exports, module) {   var exec = require('cordova/exec');   var platform = require('cordova/platform');       module.exports = {       }   });

添加函数后的hello.js如下:

cordova.define("cordova-plugin-hello.hello", function(require, exports, module) {   var exec = require('cordova/exec');   var platform = require('cordova/platform');   module.exports = {           sayHello: function () {               exec(null, null, "Hello", "sayHello", ["message", "title", "labelButton"]);               },           sayHelloWithCallBack: function(message, completeCallback, title, buttonLabel) {               var _message = (typeof message === "string" ? message : JSON.stringify(message));               var _title = (typeof title === "string" ? title : "Alert");               var _buttonLabel = (buttonLabel && typeof buttonLabel === "string" ? buttonLabel : "OK");               exec(completeCallback, null, "hello", "sayHelloWithCallBack", [_message, _title, _buttonLabel]);           }      }});

这里完成之后,一个空白的插件就创建好了;

二、双击”helloworld.xcworkspace”打开工程,删除工程与”WWW”的关联,重新引入,效果如下图:
这里写图片描述

如果有问题,解决报错(不会有严重问题),编译ok;

三、在如下图位置,创建与插件对应的原生实现类

这里写图片描述

可以在里面编写自己的函数,例子如下:

#import <Cordova/CDVPlugin.h>@interface CDVHello : CDVPlugin- (void)sayHello:(CDVInvokedUrlCommand *)command;- (void)sayHelloWithCallBack:(CDVInvokedUrlCommand *)command;@end////  CDVHello.m//  helloworld////  Created by Chenfy on 16/12/22.////#import "CDVHello.h"@interface CDVHello()@property(nonatomic,copy)NSString   *callBackId;@end@implementation CDVHello- (void)sayHello:(CDVInvokedUrlCommand *)command {    NSLog(@"Say hello Called!");    [self showMessage:@"hello!"];}- (void)showMessage:(NSString *)msg {    UIAlertView *alV = [[UIAlertView alloc]initWithTitle:@"Titls" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Sure", nil];    [alV show];}- (void)sayHelloWithCallBack:(CDVInvokedUrlCommand *)command {    NSLog(@"Say hello sayHelloWithCallBack!");    NSString* callbackId = command.callbackId;    NSString* message = [command argumentAtIndex:0];    NSString* title = [command argumentAtIndex:1];    NSString* buttons = [command argumentAtIndex:2];    self.callBackId = callbackId;    __weak CDVHello* weakHello = self;    CDVPluginResult *result;    result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:(int)(2)];    [weakHello.commandDelegate sendPluginResult:result callbackId:callbackId];}@end

四、创建关联,将hello.js同CDVHello关联起来
1:config.xml文件的配置,如下图:
这里写图片描述
2:cordova_plugins.js文件的配置,如下图:
这里写图片描述
3:测试页面的编写,如下图:
这里写图片描述

五、配置完以上步骤就可以进行测试了,测试结果如下图:

1:show hello页面

这里写图片描述

2:call hello back页面

这里写图片描述

1 0
原创粉丝点击