您的位置:首页 > 移动开发 > IOS开发

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

2015-12-24 14:39 405 查看
创建插件文件。

<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,进行精准的移动营销,为应用开发者快速提升广告的收入;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: