我想创建iPhone日历的日历条目,我尝试了以下代码
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = self.selectedPost.postTitle;
event.notes = self.selectedPost.postContent;
event.startDate = self.selectedPost.startDate;
event.endDate = self.selectedPost.endDate;
EKCalendar *targetCalendar = nil;
targetCalendar = [eventStore defaultCalendarForNewEvents];
NSLog(@"%@",targetCalendar);
[event setCalendar:targetCalendar];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
UIAlertView *alert = nil;
NSLog(@"err %@",err);
if (err) {
alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[err localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
}
else{
alert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Added to calender" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
}
[alert show];
但结果是
2013-01-15 22:31:34.682 Project[40863:907] defaultCalendarForNewEvents failed: Error Domain=EKCADErrorDomain Code=1013 "The operation couldn’t be completed. (EKCADErrorDomain error 1013.)"
2013-01-15 22:31:34.683 Project[40863:907] (null)
2013-01-15 22:31:34.690 Project[40863:907] err Error Domain=EKErrorDomain Code=1 "No calendar has been set." UserInfo=0x1d535ba0 {NSLocalizedDescription=No calendar has been set.}
我知道这是因为
[eventStore defaultCalendarForNewEvents];
返回null。 我试过了 [eventStore calendarWithIdentifier:event.calendarItemIdentifier]; 和一些其他代码但相同的结果如何解决这个问题 任何想法
答案 0 :(得分:7)
如果这是在iOS 6.0或更高版本上,您必须先使用方法-[EKEventStore requestAccessToEntityType:completion:]
查看Calendar and Reminders Programming Guide 中给出的示例答案 1 :(得分:2)
为了不浪费时间,请确保您使用-[EKEventStore requestAccessToEntityType:completion:]
中的位掩码 像这样EKEventStore *eventStore = [[EKEventStore alloc] init];
[eventStore requestAccessToEntityType:EKEntityMaskEvent completion:^(BOOL granted, NSError *error) {
// ...
}];
答案 2 :(得分:1)
添加这些行
if(eventStore.defaultCalendarForNewEvents==nil)
*eventStore = [[EKEventStore alloc] init];
第二行仅在您授予访问权限时第一次执行
您的代码应如下所示
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = self.selectedPost.postTitle;
event.notes = self.selectedPost.postContent;
event.startDate = self.selectedPost.startDate;
event.endDate = self.selectedPost.endDate;
EKCalendar *targetCalendar = nil;
if(eventStore.defaultCalendarForNewEvents==nil)
*eventStore = [[EKEventStore alloc] init];
targetCalendar = [eventStore defaultCalendarForNewEvents];
NSLog(@"%@",targetCalendar);
[event setCalendar:targetCalendar];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
UIAlertView *alert = nil;
NSLog(@"err %@",err);
if (err) {
alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[err localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
}
else{
alert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Added to calender" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
}
[alert show];
这对我有用
答案 3 :(得分:0)
我通过确保标题与我创建的标题不同来修复它,例如我在几个晚上都有dance
所以有一晚我会做{{1} }和另一个句点在开头Dance
。