ios - 如何在iOS中的特定日期添加事件?

标签 ios objective-c ekevent ekeventstore

我有一个应用程序,需要在其中添加来自服务器的特定日期的事件。我这样做如下:

[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次。谁能告诉我我要去哪里错了

我需要在特定日期添加 Activity 吗?有人可以帮我吗?

最佳答案

检查您的日期格式是否正确。

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/

关于ios - 如何在iOS中的特定日期添加事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38388131/

相关文章:

ios - 从日历中检索带有标识符的事件时应用程序崩溃

ios - EKEventViewController 不显示事件详细信息

ios - 核心数据和数组

ios - 当某些情况发生时如何停止插入segue?

objective-c - 将 Facebook 集成到 Xcode 4.3

iphone - 将简单数据写入iphone沙箱?

ios - 如何在 iphone 日历中跟踪我的应用程序添加的事件

c# - 从 URL 获取图像

ios - 类型 UIView 的值没有成员错误

ios - React Native App 未安装在 ios 模拟器中