IOS版添加phonegap--插件写法教程

创建插件文件。
<1>创建baidumap.h
#import <Foundation/Foundation.h>
#import <Cordova/CDVPlugin.h>//这是最新插件需要引入的库文件
@interface baidumap : CDVPlugin
–(void)map:(CDVInvokedUrlCommand*)command;//这是最新插件的写法
@end


<2>创建baidumap.m
 #import “baidumap.h”
#import <Cordova/CDVPlugin.h>
@implementation baidumap
–(void)map:(CDVInvokedUrlCommand *)command{
CDVPluginResult* pluginResult = nil;
NSString* echo = [command.arguments objectAtIndex:0]; //获取冲js文件传过来的值
NSLog(@”Client Information: %@”,  echo);
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


<3>在congfig.xml 文件中注册插件
<plugin name=”baidumap” value=”baidumap” />


<4>写js插件部分
 var cordovaRef = window.PhoneGap || window.Cordova || window.cordova;
function success () {
//Generic callback provided if the Cordova call to the native  Objective-C should be successful
//Note: Probably don’t want to do anything here… but provided nevertheless
}
function fail () {
//Generic callback provided if the Cordova call to the native Objective-C should fail
}
var baidumap = {
map: function(success, fail, str) {
cordova.exec(success, fail, “baidumap”, “map”, [str]);
}
};


<5>引入插件道你的index文件。
并执行以下方法:
function map(str){
baidumap.map(
function(echoValue){ alert(echoValue == “home”);
alert(echoValue);
},
function(err) { alert(err);},str);
}
<button type=”button” onClick=”map(‘home’)”>插件测试</button>


KeyMob是国内优秀的移动广告平台;为广告主、网站主和应用开发者提供专业服务。KeyMob通过稳定的广告SDK,进行精准的移动营销,为应用开发者快速提升广告的收入;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值