我使用的是Apple Sample Code.
中的日历代码我想在应用中制作我的日历以添加活动。 但我不知道如何制作自己的日历并将其添加到iOS的默认日历应用程序中。
我收到此错误。 “因未捕获的异常而终止应用程序'NSGenericException',原因:'无法直接初始化日历。使用calendarWithEventStore”
-(void)performCalendarActivity:(EKEventStore*)evtStore
{
On using
// find local source
EKSource *localSource = nil;
for (EKSource *source in self.eventStore.sources) {
if (source.sourceType == EKSourceTypeLocal) {
localSource = source;
break;
}
}
// Get the default calendar from store.
// self.defaultCalendar = [self.eventStore defaultCalendarForNewEvents];
self.defaultCalendar = [[EKCalendar alloc]init];
// self.defaultCalendar = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:self.eventStore];
self.defaultCalendar.CGColor = [UIColor blueColor].CGColor;
self.defaultCalendar.title = @"MyNEWCalForApp";
// self.defaultCalendar.allowsContentModifications = YES;
self.defaultCalendar.source = localSource;
NSError* error1;
[self.eventStore saveCalendar:self.defaultCalendar commit:YES error: &error1];
NSLog(@"error:%@",error1);
NSLog(@"save cal id = %@", self.defaultCalendar.calendarIdentifier);
[[NSUserDefaults standardUserDefaults] setObject:self.defaultCalendar.calendarIdentifier forKey:@"CalendarIdentifier"];
[[NSUserDefaults standardUserDefaults] synchronize];
self._calIdentifier = [[NSUserDefaults standardUserDefaults]valueForKey:@"CalendarIdentifier"];
NSLog(@"existing cal id = %@", [[NSUserDefaults standardUserDefaults]valueForKey:@"CalendarIdentifier"]);
self.defaultCalendar = [self.eventStore calendarWithIdentifier:_calIdentifier];
self.eventsList = [[NSMutableArray alloc] initWithArray:0];
// Fetch today's event on selected calendar and put them into the eventsList array
[self.eventsList addObjectsFromArray:[self fetchEventsForToday]];
[self.tableView reloadData];
}
的 的 * ---- ---- EDIT **
使用: self.defaultCalendar = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:self.eventStore]; 添加事件和委托调用
// Overriding EKEventEditViewDelegate method to update event store according to user actions.
- (void)eventEditViewController:(EKEventEditViewController *)controller
didCompleteWithAction:(EKEventEditViewAction)action {
NSError *error = nil;
EKEvent *thisEvent = controller.event;
switch (action) {
case EKEventEditViewActionCanceled:
// Edit action canceled, do nothing.
break;
case EKEventEditViewActionSaved:
// When user hit "Done" button, save the newly created event to the event store,
// and reload table view.
// If the new event is being added to the default calendar, then update its
// eventsList.
if (self.defaultCalendar == thisEvent.calendar) {
[self.eventsList addObject:thisEvent];
}
if(self.defaultCalendar == thisEvent.calendar){ 从来都不是真的。
请帮忙。