JAK
JAK

Reputation: 6471

How to get eventIdentifier from EKEvent store?

Now i am working on EKEvent mange in calendar application.I successfully add an event to eventStore.I need to get an event identifier from event store. I use the following code for access the eventIdentifier.But i always get null value for evetnIdentifier in my app.

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

Upvotes: 0

Views: 1431

Answers (1)

Dhruvik
Dhruvik

Reputation: 982

try this ::

in your .h

#import <EventKit/EventKit.h>

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

in your .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 ];
    }
}

Hope it'll help you. happy coding. Thanks.

Upvotes: 0

Related Questions