Add event to calendar with EventKit Mountain Lion - eventkit

I added the framework EventKit and I have this code. I can't add an event at default calendar.
#import "Calendar.h"
#import <EventKit/EventKit.h>
#implementation Calendar
-(IBAction)addCal:(id)sender
{
EKEventStore *store = [[EKEventStore alloc] initWithAccessToEntityTypes:EKEntityMaskEvent];
EKEvent *evento = [EKEvent eventWithEventStore:store];
[evento setStartDate:[NSDate date]];
[evento setTitle:#"Title"];
[evento setCalendar:[store defaultCalendarForNewEvents]];
NSError *error;
[store saveEvent:evento span:EKSpanThisEvent commit:YES error:&error];
}
In Console I have this:
"We are including and fetching events, because this application did something event-related. If this application doesn't care about events, then this is a potentially expensive call."
I'm a beginner.
Thanks for your help.

I successfully used this code:
//Calendar Test:
[self setEventStore:[[EKEventStore alloc] initWithAccessToEntityTypes:EKEntityMaskEvent]];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dc = [[NSDateComponents alloc] init];
[dc setHour:1];
NSDate *startDate = [NSDate date];
NSDate *endDate = [calendar dateByAddingComponents:dc toDate:[NSDate date] options:0];
EKEvent *anEvent = [EKEvent eventWithEventStore:eventStore];
[anEvent setTitle:#"event1"];;
[anEvent setCalendar:[[eventStore calendarsForEntityType:EKCalendarTypeLocal]objectAtIndex:0]];
[anEvent setLocation:#"Somewhere"];
[anEvent setStartDate:startDate];
[anEvent setEndDate:endDate];
[eventStore saveEvent:anEvent span:EKSpanThisEvent commit:YES error:nil];
It gives me a one-hour event in my first calendar, but I still do get the message. I suspect the message is a one-off debugging message, but I did not test with a release version of the program. Couldn't find anything about this in the docs, either.
Hope that helps.

Related

How to delete event from iCal added from my app.?

I'm developing an app in which when user add any event as his favorite, I'm adding that event to iCal but how do i remove that event from iCal if user removes that particular event from his favorite.?
Here is my code:
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[[self.parentDetailArray valueForKey:#"start_time_num"] intValue]];
NSDateFormatter *dateformatter=[[NSDateFormatter alloc]init];
[dateformatter setLocale:[NSLocale currentLocale]];
[dateformatter setDateFormat:#"dd-MM-yyyy"];
NSString *dateString=[dateformatter stringFromDate:date];
EKEventStore *eventStore = [[EKEventStore alloc] init];
if([eventStore respondsToSelector:#selector(requestAccessToEntityType:completion:)])
{
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (granted){
//---- codes here when user allow your app to access theirs' calendar.
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = title;
event.startDate = date;
event.endDate = [[NSDate alloc] initWithTimeInterval:1000 sinceDate:event.startDate];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
// Here I'm trying to get the identifier of that event but m getting only (null)
self.str = [[NSString alloc] initWithFormat:#"%#", event.eventIdentifier];
[self.arrayofEventId addObject:self.str];
//[self performCalendarActivity:eventStore];
}else
{
//----- codes here when user NOT allow your app to access the calendar.
}
}];
}
else {
//---- codes here for IOS < 6.0.
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = #"Testing for calendar";
event.startDate = [[NSDate alloc] init];
NSLog(#"%#",event.startDate);
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
//[self performCalendarActivity:eventStore];
}
How do i get the event identifier n remove that particular event from iCal. any help would be appreciated.
You could write all app generated event ID's to file and then load them back up next time the app loads. This way you can keep track of which events your app created. Then you may want to try this method:
- (BOOL)removeEvent:(EKEvent *)event span:(EKSpan)span commit:(BOOL)commit error:(NSError **)error
Save with this:
NSString *id = [[NSString alloc] initWithFormat:#"%#", event.eventIdentifier];
Remove with this:
EKEvent *event = [eventStore eventWithIdentifier:id];
NSError *error = nil;
[eventStore removeEvent:event span:EKSpanThisEvent error:&error];
Apple has documentation on the EKEventStore here.

How to make repeating calender event in iphone

Can anybody tell me how to make a repeating (weekly,daily,monthly) event for iOS Calendar events. My current implementation is something like this:
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = [cellValue objectForKey:#"ZEVENTNAME"];
event.notes = [cellValue objectForKey:#"ZEVENTDESCRIPTION"];
event.location = [cellValue objectForKey:#"ZLOCAITONname"];
NSString * dateStart =[cellValue objectForKey:#"ZEVENTSTARTDATE"];
NSString * dateEnd =[cellValue objectForKey:#"ZEVENTENDDATE"];
NSDateFormatter *_formatter=[[NSDateFormatter alloc]init];
[_formatter setDateFormat:#"yyyyMMdd"];
NSDate *sDate = [_formatter dateFromString:dateStart];
NSDate *eDate = [_formatter dateFromString:dateEnd];
[_formatter setDateFormat:#"MM/dd/yyyy"];
event.startDate = sDate;
event.endDate = eDate;
event.calendar = [eventStore defaultCalendarForNewEvents];
NSError *error;
[eventStore saveEvent:event span:EKSpanThisEvent error:&error];
I have gone through developer help from apple but couldn't get the way how to implement.
Thanks

subscribe to a url to add event in ical

I Want to add my events that is in my server into ical using eventkit. Can I subscribe to a url programatically so that all the event can be sync in ical app automatically.
Synchronization is done from your side (Inside objective "c"). You need to fetching all events from server after hitting appropriate URL.
Parse it and save single event with the help of below method:
-(void)addingEvents{
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = <<Event Title>>;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-MM-dd hh:mma"];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSString *strDate = [dateFormatter stringFromDate:<<Your event date>>];
NSDate *startDate = [dateFormatter dateFromString:strDate];
[dateFormatter release];
event.startDate = [[NSDate alloc] initWithTimeInterval:0 sinceDate:startDate];
event.endDate = [[NSDate alloc] initWithTimeInterval:600 sinceDate:event.startDate];
NSArray *alertArray = [NSArray arrayWithObject:[EKAlarm alarmWithRelativeOffset:-300]];
event.alarms = alertArray;
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
if (err) {
NSLog(#"Error occured : %#",[err localizedDescription]);
}
[eventStore release];
}

How to set an event in NSCalender?

I'm trying to add event with my calendar date like following:
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = #"Calendar";
EKEventStore *eventStore = [[EKEventStore alloc]init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = #"Training";
event.startDate = [[NSDate alloc]init];
event.endDate = [[NSDate alloc]initWithTimeInterval:600 sinceDate:event.startDate];
//set the default calendar type
calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
// set today
today = [[NSDate alloc] initWithTimeIntervalSinceNow:1];
//set the current day to show the calendar
NSDateComponents *components = [calendar components:units fromDate:[NSDate date]];
[components setDay:1];
self.currentDate = [calendar dateFromComponents:components];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
//[event setCalendar:<#(EKCalendar *)#>]
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
[self fillCalendar];
}
But not able to add a Training event in it.I think, I need to replace "defaultCalenderForNewEvents" with my my [self fillCalender].But, this is not working.Could you please suggest ? Thanks.
In my code which creates events I do the folliwing:
NSDate * startDate = [self.complexDatePicker date];
EKEventStore *store = [[EKEventStore alloc] init];
EKEvent * event = [EKEvent eventWithEventStore:store];
EKCalendar * defCalendar = [store defaultCalendarForNewEvents];
[event setCalendar:defCalendar];
event.startDate = startDate;
event.endDate = [NSDate dateWithTimeInterval:3600 sinceDate:[NSDate date]];
event.title = #"SeeksPack Exercises";
//Temporary hardcoded day values, user will be able to select exact days
EKRecurrenceDayOfWeek * day2 = [[EKRecurrenceDayOfWeek alloc] initWithDayOfTheWeek:3 weekNumber:0];
EKRecurrenceDayOfWeek * day4 = [[EKRecurrenceDayOfWeek alloc] initWithDayOfTheWeek:5 weekNumber:0];
EKRecurrenceDayOfWeek * day6 = [[EKRecurrenceDayOfWeek alloc] initWithDayOfTheWeek:7 weekNumber:0];
EKRecurrenceRule * rule = [[EKRecurrenceRule alloc] initRecurrenceWithFrequency:EKRecurrenceFrequencyDaily interval:1 daysOfTheWeek:[NSArray arrayWithObjects:day2, day4, day6, nil] daysOfTheMonth:nil monthsOfTheYear:nil weeksOfTheYear:nil daysOfTheYear:nil setPositions:nil end:[EKRecurrenceEnd recurrenceEndWithOccurrenceCount:25]];
event.recurrenceRules = [NSArray arrayWithObject:rule];
NSError * er;
[store saveEvent:event span:EKSpanFutureEvents commit:YES error:&er];
//The identifier should actually be stored in app bundle in order to get and modify already created events
NSString * identif = event.eventIdentifier;
Take a look at the method which actually saves the event it is:
- (BOOL)saveEvent:(EKEvent *)event span:(EKSpan)span commit:(BOOL)commit error:(NSError **)error
And as apple doc says:
Save and delete events from the event store with the saveEvent:span:commit:error: and removeEvent:span:commit:error: methods.
So try to change the method of saving may be this help you
Hope the below code may help you
EKEventStore *eventStore = [[[EKEventStore alloc] init] autorelease];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
EKReminder *reminder = [EKReminder reminderWithEventStore:eventStore];
event.title = #"Test Event";
reminder.title = #"Test reminder";
NSDate *cDate = [NSDate date];
NSLog(#"current date %#", cDate);
/*NSDateComponents *startDateComponents = [[[NSDateComponents alloc] init] autorelease];
[startDateComponents setDay:12];
[startDateComponents setMonth:12];
[startDateComponents setYear:2012];
[startDateComponents setHour:12];
[startDateComponents setMinute:18];
NSDateComponents *endDateComponents = [[[NSDateComponents alloc] init] autorelease];
[endDateComponents setDay:12];
[endDateComponents setMonth:12];
[endDateComponents setYear:2012];
[endDateComponents setHour:12];
[endDateComponents setMinute:18];
[endDateComponents setSecond:20];*/
//event.startDate = cDate;
//event.endDate = [cDate dateByAddingTimeInterval:15.0];
event.startDate = [cDate dateByAddingTimeInterval:((5*60*60) + (30 * 60) + 15)];
event.endDate = [cDate dateByAddingTimeInterval:((5*60*60) + (30 * 60) + 30)];
//event.startDate = [[NSCalendar currentCalendar] dateFromComponents:startDateComponents];
//event.endDate = [[NSCalendar currentCalendar] dateFromComponents:endDateComponents];
reminder.completionDate = [cDate dateByAddingTimeInterval:((5*60*60) + (30 * 60) + 10)];
NSLog(#"startdate %#", event.startDate);
NSLog(#"enddate %#", event.endDate);
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
//[reminder setCalendar:[eventStore defaultCalendarForNewReminders]];
NSError *error = nil;
[eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&error];
//[eventStore saveReminder:reminder commit:YES error:&error];
start date not assigned...
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = #"My Event Name";
event.startDate =[NSDate date];
event.endDate = [[NSDate alloc] initWithTimeInterval:60 sinceDate:event.startDate];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];

Cleaning up memory after using EKEvent

Was looking at some code and it looks to be leaking memory. And I'm not sure should I clean this up? Or is it ok?
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
I would have guessed this is an autorelease since its a connivence method.
But when i read
event.startDate = [[NSDate alloc] init];
I see an alloc and an init, so I get nervous about wondering will it be leaking.
Full block of code below:
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = #"Test Event for Code Demo";
event.startDate = [[NSDate alloc] init];
event.endDate = [[NSDate alloc] initWithTimeInterval:600 sinceDate:event.startDate];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
[eventStore release];
Thanks,
-Code
I don't think the properties startDate and endDate need to be alloc'd and init'd. You are creating an autoreleased object with your current code.
EKEvent *event = [EKEvent eventWithEventStore:eventStore]; // autoreleased this way
To fill your dates and properties, try an alternate method to fill.
event.startDate = [NSDate date];
event.endDate = [NSDate dateWithTimeInterval:600 sinceDate:event.startDate];
Now you just need to release eventStore like you are currently doing. Hope this helps.
If you are using EKEventViewController, Apple documentation says following:
#property(nonatomic, retain) EKEvent *event
Discussion
This property must be set before the view is displayed.
EKEventViewController Documentation