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

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

如下图:
这里写图片描述

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

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

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

//hello.js
cordova.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页面

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值