您现在的位置是:首页 > 电脑技术查询 > web开发

混同开发-利用Cordova插件实现HTML5 与 原生代码的连接

编辑:chaxungu时间:2022-10-10 23:23:35分类:web开发

混合开发-利用Cordova插件实现HTML5 与 原生代码的连接

主要利用的就是通过Cordova这个东西, 进行HTML5 与 iOS断值得传递


列子:把HTML5 获取的日期同步到iOS系统的日历中去

1. 原生代码:.h.m 提供一个接口文件



主要代码:

- (BOOL)saveEventToCalender:(NSString *)title content:(NSString *)contentTitle year:(NSString *)year date:(NSString *)date time:(NSString *)time

{

// 定义一个事件市场

EKEventStore *eventStore = [[EKEventStorealloc] init];

if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {

[eventStore requestAccessToEntityType:EKEntityTypeEventcompletion:^(BOOL granted,NSError * _Nullable error) {

dispatch_async(dispatch_get_main_queue(), ^{

if (error) {

flag = 0;

}

else if (!granted){

// 用户不允许访问日历

flag = 0;

}else {

// 事件保存到日历中

flag = 1;

// 创建日历

EKEvent *event = [EKEventeventWithEventStore:eventStore];

// 设置保存事件的内容或者题目

event.title = title;

event.location = contentTitle;

// 进行日期字符串的拼接操作

if (![date isEqualToString:@""] && ![time isEqualToString:@""]) {

NSArray *dateArray = [datecomponentsSeparatedByString:@"."];

NSArray *timeArray = [timecomponentsSeparatedByString:@":"];

NSInteger timeDate = [timeArray[0]integerValue] + 2;

NSString *timeFinally = [NSStringstringWithFormat:@"%ld", (long)timeDate];

NSString *startTime = [NSStringstringWithFormat:@"%@年%@月%@日 %@:%@", year, dateArray[0], dateArray[1], timeArray[0], timeArray[1]];

NSString *endTime = [NSStringstringWithFormat:@"%@年%@月%@日 %@:%@", year, dateArray[0], dateArray[1], timeFinally, timeArray[1]];


NSDateFormatter *tempFormatter = [[NSDateFormatteralloc] init];

// 设置时间的格式,可以重新自定义

[tempFormatter setDateFormat:@"yyyy年MM月dd日 HH:mm"];

event.startDate = [[NSDatealloc] init];

NSDate *dateTime = [[tempFormatterdateFromString:startTime] dateByAddingTimeInterval:[NSTimeZone localTimeZone].secondsFromGMT];

NSDate *dateTimeEnd = [[tempFormatterdateFromString:endTime] dateByAddingTimeInterval:[NSTimeZone localTimeZone].secondsFromGMT];

event.startDate = dateTime;

// 设置结束的时间(一定要加结束的时间)

// event.endDate = [[NSDate alloc] init];

event.endDate = dateTimeEnd;

event.allDay = NO;

// 增加提醒(可以重新设置)

[event addAlarm:[EKAlarmalarmWithRelativeOffset:60.0f * -60.0f *24]];

[event addAlarm:[EKAlarmalarmWithRelativeOffset:60.0f * -15.0f]];

[event setCalendar:[eventStoredefaultCalendarForNewEvents]];

NSError *err;

[eventStore saveEvent:eventspan:EKSpanThisEventcommit:YES error:&err];

}


}

});

}];

}

return flag;


}



2. 对原生代码的一个封装, 用于与HTML5信息的传递, 原生Cordova插件的封装


创建一个文件继承于

CDVPlugin


引入:

#import <Cordova/CDVPlugin.h>

主要代码:


- (void)addconference:(CDVInvokedUrlCommand*)command{

ETLog(@"_________conmmand___________%@",command.arguments);

NSString *title = command.arguments[0];

EventToCalander *calander = [[EventToCalanderalloc] init];

[calander saveEventToCalender:command.arguments[1]content:command.arguments[2]year:command.arguments[3]date:command.arguments[4]time:command.arguments[5]];

NSMutableDictionary *param_info = [[NSMutableDictionaryalloc] init];


[param_info setValue:@"ok"forKey:@"back"];


CDVPluginResult* pluginResult = [CDVPluginResultresultWithStatus:CDVCommandStatus_OKmessageAsDictionary:param_info];

[self.commandDelegatesendPluginResult:pluginResult callbackId:command.callbackId];


}

3. 配置config.xml文件 如原生代码与web段建立关联

<feature name="LoginInfo_intent">

<param name="ios-package"value="ETELoginInfo" />

</feature>

4. 设置配置文件在cordova.js 文件中

// 配置文件配置的方法

{

"file":"plugins/com.etteacher.plugins/www/LoginInfo_intent.js",

"id": "org.apache.cordova.LoginInfo_intent",

"merges": [

"navigator.LoginInfo_intent"

]

},


其中: file 指的是Plugins 的路径id: 文件识别的idmerges:调用方法的名称5. 设置Plugins 中的内容

cordova.define("org.apache.cordova.LoginInfo_intent",function (require, exports, module) {


var exec = require('cordova/exec');


module.exports = {

demo: function (sucess,failed,mills) {

exec(sucess, failed, "LoginInfo_intent","intent", [mills]);

},



wifi: function (success,failed,mills) {

exec(success, failed, "LoginInfo_intent","wifi", [mills]);

},


addconference: function (success,failed,mills) {

exec(success, failed, "Add_meeting","addconference", mills);

},


openPDF: function (success,failed,mills) {

exec(success, failed, "LoginInfo_intent","openPDF", mills);

}

,

timerpick: function(success,failed,mills) {

exec(success, failed, "LoginInfo_intent","timerpick", mills);

},

getproductinfo: function(success,failed,mills) {

exec(success, failed, "LoginInfo_intent","get_productinfo", mills);

}

};


});



其中有分别实现几个功能的方法
6. web段HTML5 利用AngularJS 进行传值的操作

if(navigator.LoginInfo_intent){

navigator.LoginInfo_intent.addconference(function (winParam) {



//myAlert(winParam.back);

logger("winParam_back----->" + winParam.back);


}, null, [cityAddress,project_name,$scope.cityDetailAddress,year_sel,date_sel,time_sel,user_name,user_email]);

}

}



其中为主要代码, , 整个思路就是web 段与 iOS段进行传值的过程, 其中需要设置一些关于Cordova的配置文件