引入
#import <EventKit/EventKit.h>
记得要配置隐私权限 否则会崩溃Privacy - Calendars Usage Description
在URL Shemes中定义一个名字
eg:openMySelf
在代码中使用
- (void)method2 {
//事件市场
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
}
else
{
// access granted
// ***** do the important stuff here *****
//事件保存到日历
//创建事件
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = @"open";
event.location = @"位置";
//主要是这句code
event.notes = @"openMySelf://xxxxx?";
NSDateFormatter *tempFormatter = [[NSDateFormatter alloc]init];
[tempFormatter setDateFormat:@"dd.MM.yyyy HH:mm"];
event.startDate = [[NSDate alloc]init ];
event.endDate = [[NSDate alloc]init ];
event.allDay = YES;
//添加提醒
[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 error:&err];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Event Created"
message:@"Yay!?"
delegate:nil
cancelButtonTitle:@"Okay"
otherButtonTitles:nil];
[alert show];
NSLog(@"保存成功");
}
});
}];
}
else
{
// ios 5 一下未测试
// this code runs in iOS 4 or iOS 5
// ***** do the important stuff here *****
//4.0和5.0通过下述方式添加
//保存日历
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = @"open";
event.location = @"位置";
NSDateFormatter *tempFormatter = [[NSDateFormatter alloc]init];
[tempFormatter setDateFormat:@"dd.MM.yyyy HH:mm"];
event.startDate = [[NSDate alloc]init ];
event.endDate = [[NSDate alloc]init ];
event.allDay = YES;
[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 error:&err];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Event Created"
message:@"Yay!?"
delegate:nil
cancelButtonTitle:@"Okay"
otherButtonTitles:nil];
[alert show];
NSLog(@"保存成功");
}
}