
If you would need to programmatically set an event in the users Calendar you’ll need to leverage the EventKit framework API. The EventKit framework provides classes for accessing and manipulating calendar events and reminders.
First, access your target settings, click on “Summary”, scroll down to the “Linked Frameworks and Libraries” group and click the + button to add a new framework to your project. Search for ‘EventKit.Framework’.
Then you will be able to add a method like the one below to your class.
First add the EventKit framework to the top of your *.m file.
#import
Next use the method below to set the iCal event.
-(void) setiCalEvent { // Set the Date and Time for the Event NSDateComponents *comps = [[NSDateComponents alloc] init]; [comps setYear:2013]; [comps setMonth:3]; [comps setDay:5]; [comps setHour:9]; [comps setMinute:0]; [comps setTimeZone:[NSTimeZone systemTimeZone]]; NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDate *eventDateAndTime = [cal dateFromComponents:comps]; // Set iCal Event EKEventStore *eventStore = [[EKEventStore alloc] init]; EKEvent *event = [EKEvent eventWithEventStore:eventStore]; event.title = @"EVENT TITLE"; event.startDate = eventDateAndTime; event.endDate = [[NSDate alloc] initWithTimeInterval:600 sinceDate:event.startDate]; // Check if App has Permission to Post to the Calendar [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) { if (granted){ //---- code here when user allows your app to access their calendar. [event setCalendar:[eventStore defaultCalendarForNewEvents]]; NSError *err; [eventStore saveEvent:event span:EKSpanThisEvent error:&err]; }else { //----- code here when user does NOT allow your app to access their calendar. [event setCalendar:[eventStore defaultCalendarForNewEvents]]; NSError *err; [eventStore saveEvent:event span:EKSpanThisEvent error:&err]; } }]; }
To customize it for your app, set the date and time of the iCal event, then change the event.title to what you want the user to see in their calendar, and finally the length of the event in seconds (i.e. 600 secs = 1 hour).
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) { if (granted){ //---- code here when user allows your app to access their calendar. }else { //----- code here when user does NOT allow your app to access their calendar. } }];
On iOS6, Apple introduced a new privacy control. The user can control permissions for the contact and calender by each app. So in the code side, you need to add some way to request the permission. In iOS5 or before, you can always call it without the user having to give permission.