iOS 添加事件到系统日历中
2016-07-09 11:09
465 查看
-(void)saveEventToCalendarTomorrow:(id)event{
//事件市场
EKEventStore *eventStore = [[EKEventStore
alloc] init];
//6.0及以上通过下面方式写入事件
if ([eventStore
respondsToSelector:@selector(requestAccessToEntityType:completion:)])
{
// the selector is available, so we must be on iOS 6 or newer
[eventStore requestAccessToEntityType:EKEntityTypeEvent
completion:^(BOOL granted,
NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error)
{
//错误细心
// display error message here
}
else
if (!granted)
{
//被用户拒绝,不允许访问日历
// display access denied error message here
UIAlertView *alert=[[UIAlertView
alloc] initWithTitle:@"提醒"
message:@"请设置本应用日历权限"
delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:nil,
nil];
[alert show];
}
else
{
// access granted
// ***** do the important stuff here *****
//事件保存到日历
//创建事件
EKEvent *event = [EKEvent
eventWithEventStore:eventStore];
event.title =
@"我是日历事件啊";
event.location =
@"我在shanghai";
event.notes =@"notes notes notes notes notes notes notes notesnotes notes notes notes notes notes notes notes notes notes notes notes
notes notesnotes notes notes notes notes notes notes notes notes notes notes notes notes notesnotes notes notes notes notes notes notes notes notes notes notes notes notes notesnotes notes notes notes notes notes notes notes notes notes notes notes notes notesnotes
notes notes notes notes notes notes notes notes notes notes notes notes notesnotes notes notes notes notes notes notes notes notes notes notes notes notes notesnotes notes notes notes notes notes notes notes notes notes notes notes notes notesnotes notes notes
notes notes notes notes notes notes notes notes notes notes notesnotes notes notes notes notes notes notes notes notes notes notes notes notes notesnotes notes notes notes notes notes notes notes notes notes notes notes notes notesnotes notes notes notes notes
notes notes notes notes notes notes notes notes notesnotes notes notes notes notes notesnotes notes notes notes notes notes notes notesnotes notes notes notes notes notes";
//日历中显示的url
event.URL=[NSURL
URLWithString:@"http://www.baidu.com"];
NSDateFormatter *tempFormatter = [[NSDateFormatter
alloc]init];
[tempFormatter setDateFormat:@"dd.MM.yyyy HH:mm"];
//创建一个时间段的日历事件
NSString *startDateStr=@"08.07.2016 10:10";
NSString *endDateStr=@"08.07.2016 11:50";
event.startDate=[tempFormatter
dateFromString:startDateStr];
event.endDate=[tempFormatter
dateFromString:endDateStr];
//创建一个一天的日历事件
// event.startDate = [[NSDate alloc]init ];
// event.endDate = [[NSDate alloc]init ];
// event.allDay = YES;
// NSDate *currentDate=[NSDate date];
// NSLog(@"Current Date is %@ ",[tempFormatter stringFromDate:currentDate]);
// //添加提醒
// NSDate *laterDate=[NSDate dateWithTimeIntervalSinceNow:60];
// NSLog(@"laterDate Date is %@ ",[tempFormatter stringFromDate:laterDate]);
NSString *alarmDateStr=@"08.07.2016 07:10";
// [event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -60.0f * 24]];
// [event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -15.0f]];
//给事件
添加一个提醒 这样显示的是事件前多长时间提醒
比如1小时前
[event addAlarm:[EKAlarm
alarmWithRelativeOffset:-(60*60)]];
//这样显示的就是具体什么时候开始提醒
// [event addAlarm:[EKAlarm alarmWithAbsoluteDate:[tempFormatter dateFromString:alarmDateStr]]];
[event setCalendar:[eventStore
defaultCalendarForNewEvents]];
NSError *err;
//需要保存事件
才会添加到日历中去
[eventStore saveEvent:event
span:EKSpanThisEvent
error:&err];
UIAlertView *alert = [[UIAlertView
alloc]
initWithTitle:@"提醒"
message:@"创建成功?"
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
NSLog(@"保存成功");
}
});
}];
}
}
//事件市场
EKEventStore *eventStore = [[EKEventStore
alloc] init];
//6.0及以上通过下面方式写入事件
if ([eventStore
respondsToSelector:@selector(requestAccessToEntityType:completion:)])
{
// the selector is available, so we must be on iOS 6 or newer
[eventStore requestAccessToEntityType:EKEntityTypeEvent
completion:^(BOOL granted,
NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error)
{
//错误细心
// display error message here
}
else
if (!granted)
{
//被用户拒绝,不允许访问日历
// display access denied error message here
UIAlertView *alert=[[UIAlertView
alloc] initWithTitle:@"提醒"
message:@"请设置本应用日历权限"
delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:nil,
nil];
[alert show];
}
else
{
// access granted
// ***** do the important stuff here *****
//事件保存到日历
//创建事件
EKEvent *event = [EKEvent
eventWithEventStore:eventStore];
event.title =
@"我是日历事件啊";
event.location =
@"我在shanghai";
event.notes =@"notes notes notes notes notes notes notes notesnotes notes notes notes notes notes notes notes notes notes notes notes
notes notesnotes notes notes notes notes notes notes notes notes notes notes notes notes notesnotes notes notes notes notes notes notes notes notes notes notes notes notes notesnotes notes notes notes notes notes notes notes notes notes notes notes notes notesnotes
notes notes notes notes notes notes notes notes notes notes notes notes notesnotes notes notes notes notes notes notes notes notes notes notes notes notes notesnotes notes notes notes notes notes notes notes notes notes notes notes notes notesnotes notes notes
notes notes notes notes notes notes notes notes notes notes notesnotes notes notes notes notes notes notes notes notes notes notes notes notes notesnotes notes notes notes notes notes notes notes notes notes notes notes notes notesnotes notes notes notes notes
notes notes notes notes notes notes notes notes notesnotes notes notes notes notes notesnotes notes notes notes notes notes notes notesnotes notes notes notes notes notes";
//日历中显示的url
event.URL=[NSURL
URLWithString:@"http://www.baidu.com"];
NSDateFormatter *tempFormatter = [[NSDateFormatter
alloc]init];
[tempFormatter setDateFormat:@"dd.MM.yyyy HH:mm"];
//创建一个时间段的日历事件
NSString *startDateStr=@"08.07.2016 10:10";
NSString *endDateStr=@"08.07.2016 11:50";
event.startDate=[tempFormatter
dateFromString:startDateStr];
event.endDate=[tempFormatter
dateFromString:endDateStr];
//创建一个一天的日历事件
// event.startDate = [[NSDate alloc]init ];
// event.endDate = [[NSDate alloc]init ];
// event.allDay = YES;
// NSDate *currentDate=[NSDate date];
// NSLog(@"Current Date is %@ ",[tempFormatter stringFromDate:currentDate]);
// //添加提醒
// NSDate *laterDate=[NSDate dateWithTimeIntervalSinceNow:60];
// NSLog(@"laterDate Date is %@ ",[tempFormatter stringFromDate:laterDate]);
NSString *alarmDateStr=@"08.07.2016 07:10";
// [event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -60.0f * 24]];
// [event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -15.0f]];
//给事件
添加一个提醒 这样显示的是事件前多长时间提醒
比如1小时前
[event addAlarm:[EKAlarm
alarmWithRelativeOffset:-(60*60)]];
//这样显示的就是具体什么时候开始提醒
// [event addAlarm:[EKAlarm alarmWithAbsoluteDate:[tempFormatter dateFromString:alarmDateStr]]];
[event setCalendar:[eventStore
defaultCalendarForNewEvents]];
NSError *err;
//需要保存事件
才会添加到日历中去
[eventStore saveEvent:event
span:EKSpanThisEvent
error:&err];
UIAlertView *alert = [[UIAlertView
alloc]
initWithTitle:@"提醒"
message:@"创建成功?"
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
NSLog(@"保存成功");
}
});
}];
}
}
相关文章推荐
- iOS tableViewCell复用问题
- iOS 获取年月日
- 升级Xcode8后的相机crash问题-IOS10权限问题
- ios 仿京东商品详情(背景缩小,下部弹出功能)
- iOS开发 刮刮乐效果的实现
- iOS开发之--XCode快捷键
- ios 导航栏偏移-markdown版
- 在mac上安装让rvm及cocoa pods详解
- 第三方框架整理
- iOS 给控件 画边框
- 随笔
- iOS,StoryBoard适配实现
- 李洪强iOS开发之【零基础学习iOS开发【01-前言】03-前景和难易度分析
- iOS中加载HTML数据,并点击图片或链接进行相应的操作
- IOS程序启动过程
- IOS使用MKNetworkKit框架实现下载数据和上传数据
- 李洪强iOS开发之OC[013] -类的创建的练习
- 李洪强iOS开发之OC[012] -类的声明实现小结
- 即将到来的Xcode8 都更新了什么?
- IOS开发storyboard创建controller黑屏以及控件不显示问题。