标签:
主要利用的就是通过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 = [[EKEventStore alloc] init];
if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(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 = [EKEvent eventWithEventStore:eventStore];
// 设置保存事件的内容或者题目
event.title = title;
event.location = contentTitle;
// 进行日期字符串的拼接操作
if (![date isEqualToString:@""] && ![time isEqualToString:@""]) {
NSArray *dateArray = [date componentsSeparatedByString:@"."];
NSArray *timeArray = [time componentsSeparatedByString:@":"];
NSInteger timeDate = [timeArray[0] integerValue] + 2;
NSString *timeFinally = [NSString stringWithFormat:@"%ld", (long)timeDate];
NSString *startTime = [NSString stringWithFormat:@"%@年%@月%@日 %@:%@", year, dateArray[0], dateArray[1], timeArray[0], timeArray[1]];
NSString *endTime = [NSString stringWithFormat:@"%@年%@月%@日 %@:%@", year, dateArray[0], dateArray[1], timeFinally, timeArray[1]];
NSDateFormatter *tempFormatter = [[NSDateFormatter alloc] init];
// 设置时间的格式, 可以重新自定义
[tempFormatter setDateFormat:@"yyyy年MM月dd日 HH:mm"];
event.startDate = [[NSDate alloc] init];
NSDate *dateTime = [[tempFormatter dateFromString:startTime] dateByAddingTimeInterval:[NSTimeZone localTimeZone].secondsFromGMT];
NSDate *dateTimeEnd = [[tempFormatter dateFromString:endTime] dateByAddingTimeInterval:[NSTimeZone localTimeZone].secondsFromGMT];
event.startDate = dateTime;
// 设置结束的时间(一定要加结束的时间)
// event.endDate = [[NSDate alloc] init];
event.endDate = dateTimeEnd;
event.allDay = NO;
// 增加提醒(可以重新设置)
[event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -60.0f * 24]];
[event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -15.0f]];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent commit: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 = [[EventToCalander alloc] 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 = [[NSMutableDictionary alloc] init];
[param_info setValue:@"ok" forKey:@"back"];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:param_info];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
<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"
]
},
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);
}
};
});
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]);
}
}
混合开发-利用Cordova插件实现HTML5 与 原生代码的连接
标签:
原文地址:http://blog.csdn.net/zhangzhipengzhangzhi/article/details/51363094