我有一个应用程序,我需要在服务器中添加特定日期的事件。我是这样做的:
[dateFormatter setDateFormat:@"MM/dd/yyyy hh:mm:ss a"];
NSDate *strtdate=[dateFormatter dateFromString:[replacedDict valueForKey:@"Departure"]];
NSPredicate *predicateForEventOnDate = [store predicateForEventsWithStartDate:strtdate endDate:[strtdate dateByAddingTimeInterval:60*60*24] calendars:calendarArray];
[store enumerateEventsMatchingPredicate:predicateForEventOnDate usingBlock:^(EKEvent *event1, BOOL *stop) {
NSLog(@"title: %@",event1.title);
NSLog(@"hasNotes: %s",event1.hasNotes ? "YES":"NO");
NSLog(@"notes: %@",event1.notes);
NSLog(@"-----");
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:event1.title, @"title",event1.hasNotes ? event1.notes : @"",@"notes", nil];
[eventOnDate addObject:dict];
}];
NSLog(@"%@",eventOnDate);
if (eventOnDate.count > 0)
{
for (int i=0;i<[eventOnDate count];i++) {
if (![[[eventOnDate objectAtIndex:i] valueForKey:@"notes"] isEqualToString:note])
{
EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = subject; //give event title you want
event.notes = note ;
event.startDate = strtdate;
event.endDate = [event.startDate dateByAddingTimeInterval:60*60*24];
event.calendar = [store defaultCalendarForNewEvents];
NSError *err = nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
if (!err) {
}
else
{
// NSLog(@" Event not created");
}
}
else if ([[[eventOnDate objectAtIndex:i]valueForKey:@"notes"] isEqualToString:note])
{
}
}
}
但是这里事件只被添加到今天的日期,我也收到警告消息,例如Cal Database Change Notification已经改变了180次这样的通知,。任何人都可以告诉我哪里出错了
我需要将事件添加到特定日期吗?有人可以帮我吗?
答案 0 :(得分:0)
检查您的日期格式是否正确。
event.startDate应为NSDate
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted) return;
EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = @"Event Title";
event.startDate = [NSDate date]; // today
event.endDate = [event.startDate dateByAddingTimeInterval:60*60]; // Duration 1 hr
[event setCalendar:[store defaultCalendarForNewEvents]];
NSError *err = nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
NSString *savedEventId = event.eventIdentifier; // Store this so you can access this event later
}];
阅读此博客......
http://samwize.com/2014/07/09/how-to-add-edit-and-remove-ios-calendar-events/
https://developer.apple.com/library/ios/documentation/EventKit/Reference/EKEventStoreClassRef/