主要利用的就是通过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];
}
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: 文件识别的id
merges:调用方法的名称
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的配置文件