HY
我有这个代码用于向日历添加事件,但它没有添加。
-(void)event
{
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = @"Event";
NSDateFormatter *tempFormatter = [[NSDateFormatter alloc]init];
[tempFormatter setDateFormat:@"dd.MM.yyyy HH:mm"];
NSString *dateandtime =[NSString stringWithFormat:@"%@%@%@",datestring,@" ",starttimestring];
NSString *dateandtimeend =[NSString stringWithFormat:@"%@%@%@",datestring,@" ",endtimestring];
event.startDate = [tempFormatter dateFromString:dateandtime];
event.endDate = [tempFormatter dateFromString:dateandtimeend];
[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];
}
从XML中我得到这种格式的日期和时间:
datestring:28.10.2012
starttimestring:15:00
答案 0 :(得分:24)
您是在iOS 6模拟器上还是在iOS 6设备上?如果是这样,您需要在用户保存项目之前询问用户是否允许使用事件存储。
基本上,如果您的事件存储对象上有requestAccessToEntityType:completion:选择器,则调用该方法并提供在用户授予权限时执行的代码块,然后您将在该块中执行事件保存
首先将EventKit框架添加到您的项目中,不要忘记包含导入:
#import <EventKit/EventKit.h>
以下是我用过的代码片段:
EKEventStore *eventStore = [[EKEventStore alloc] init];
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 *****
}
});
}];
}
else
{
// this code runs in iOS 4 or iOS 5
// ***** do the important stuff here *****
}
[eventStore release];
这是我在这个主题上做过的博文:
http://www.dosomethinghere.com/2012/10/08/ios-6-calendar-and-address-book-issues/
答案 1 :(得分:2)
1)添加 Eventkit 框架和#import <EventKit/EventKit.h>
2)
-(void)syncWithCalendar {
EKEventStore *store = [EKEventStore new];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted) { return; }
EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = @"Event Title Testing"; //give event title you want
event.startDate = [NSDate date];
event.endDate = [event.startDate dateByAddingTimeInterval:60*60];
event.calendar = [store defaultCalendarForNewEvents];
NSError *err = nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
}];
}
3)通话功能
[self syncWithCalendar];