Cleaning up memory after using EKEvent - iphone

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

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

EKEventStore removeEvent EKErrorDomain Code=11 EKErrorObjectBelongsToDifferentStore

I am getting an error:
removing event error: Error Domain=EKErrorDomain Code=11 "That event does not belong to that event store." UserInfo=0x1fdf96b0 {NSLocalizedDescription=That event does not belong to that event store.
When I try to remove an EKEvent I just created.
The code below shows that I am storing the eventIdentifier and using it to retrieve the event. Furthermore when I do this, and NSLog the event I can see all the properties of it correctly.
From all the examples I have seen I am doing everything correctly. I also NSLog'ed the EKEventStore's eventStoreIdentifier and its the same every time I access it in any of my methods so it should be the same EKEventStore.
Any help would be greatly appreciated.
- (EKEvent *) getCurrentCalendarEvent {
NSString *currentCalendarEventID = [[UserModel sharedManager] currentCalendarEventID];
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [eventStore eventWithIdentifier:currentCalendarEventID];
return event;
}
- (void) removeCurrentCalendarEvent {
EKEvent *event = [self getCurrentCalendarEvent];
if (event) {
NSError *error;
EKEventStore *eventStore = [[EKEventStore alloc] init];
[eventStore removeEvent:event span:EKSpanFutureEvents error:&error];
}
}
- (void) addCurrentCalendarEvent {
[self removeCurrentCalendarEvent];
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = [[UserModel sharedManager] reminderLabel];
event.notes = #"Notes";
NSDate *startDate = [NSDate date];
int futureDateSecs = 60 * 5;
NSDate *endDate = [startDate dateByAddingTimeInterval:futureDateSecs];
event.startDate = startDate;
event.endDate = endDate;
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *error;
[eventStore saveEvent:event span:EKSpanThisEvent error:&error];
[[UserModel sharedManager] setCurrentCalendarEventID:event.eventIdentifier];
[[UserModel sharedManager] saveToDefaults];
}
This is happening because you are always initializing a new instance of EKEventStore. When you are adding EKEvent to EKEventStore then the instance of EKEventStore is different then when you are trying to remove. What you can do is that declare EKEventStore reference variable in .h and initialize it only one time.
in .h -
EKEventStore *eventStore;
in .m -
inside viewDidLoad -
eventStore = [[EKEventStore alloc] init];
then remove this line from all of three methods-
EKEventStore *eventStore = [[EKEventStore alloc] init];

Why can't I code an EKEvent title?

I am using the following code to create an event within the iPhone's calendar;
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = #"DHSB Assignment: %#", Assignment1.text;
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];
[EKEventStore release];
NSLog(#"Successfully added '%#' to the calendar", Assignment1.text);
Why is this saving an event with the name "DHSB Assignment: %#" rather than "DHSB Assignment: Example Text"?
Thank you.
event.title = [NSString stringWithFormat:#"DHSB Assignment: %#", Assignment1.text];
Your current code is equivalent to
[event setTitle:#"DHSB Assignment: %#"];
[Assignment1 text];
…which is valid, so it compiles and runs fine even though it isn’t doing what you want.

Multi-day event in iPhone calendar

I want to add an event but the event is added in all the days. If I add an event which has a last date after 10 days, then the event is added today and all the days between today and 10 days from now. I want to just add the event at the end date, not between these days. How can I do this?
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = appDelegate.Name;
event.startDate = [[NSDate alloc] init];
event.endDate = appDelegate.Date_iCal;
//event.allDay = NO;
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
you have to specify the correct start date.
Right now you do this
event.startDate = [[NSDate alloc] init]; // today
event.endDate = appDelegate.Date_iCal; // in the future
and this will obviously add an event that starts now and ends somewhere in the future.
I don't know anything about your event, so you have to figure out the start and end of it on your own.