0

现在我正在日历应用程序中处理 EKEvent mange。我成功地将一个事件添加到 eventStore。我需要从事件存储中获取一个事件标识符。我使用以下代码访问 eventIdentifier。但我的应用程序中的 evetnIdentifier 始终为空值。

EKEvent *event = [self eventAtIndexPath:indexPath];    
NSString *eventIdentifier]; = [[NSString alloc] initWithFormat:@"%@",event.eventIdentifier];
4

1 回答 1

0

尝试这个 ::

在你的 .h

#import <EventKit/EventKit.h>

@property (nonatomic, retain) EKEventStore *eventStore;
@property (nonatomic, retain) EKCalendar *defaultCalendar;
@property (nonatomic, retain) NSMutableArray *eventsList;

在你的 .m

- (void) fetchevents
{

    eventStore = [[EKEventStore alloc] init];

    eventsList = [[NSMutableArray alloc] init];

    EKEventStore *store = [[EKEventStore alloc] init];

    [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
        // handle access here
    }];

    // Get the appropriate calendar
NSCalendar *calendar = [NSCalendar currentCalendar];

    // Create the start date components
    NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init];
    oneDayAgoComponents.day = -1; // From which date you have to fetch Events from calander

    NSDate *oneDayAgo = [calendar dateByAddingComponents:oneDayAgoComponents
                                              toDate:[NSDate date]
                                             options:0];

    NSLog(@"%@", oneDayAgo);

    // Create the end date components
    NSDateComponents *oneYearFromNowComponents = [[NSDateComponents alloc] init];
    oneYearFromNowComponents.year = 0;
    NSDate *oneYearFromNow = [calendar dateByAddingComponents:oneYearFromNowComponents
                                                   toDate:[NSDate date]
                                                  options:0];
    NSLog(@"%@", oneYearFromNow);

    //Create the predicate from the event store's instance method
    NSPredicate *predicate = [store predicateForEventsWithStartDate:oneDayAgo endDate:oneYearFromNow                                                                                      calendars:nil];


    NSArray *events_Array = [eventStore eventsMatchingPredicate: predicate];

    for (EKEvent *eventToCheck in events_Array)
    {
       [eventsList addObject:eventToCheck.calendarItemIdentifier ];
    }
}

希望它会帮助你。快乐编码。谢谢。

于 2013-03-20T10:44:57.363 回答