Add event to Birthday calendar in iOS - iphone

I wrote this code but it doesn't work, can you help me please? I'm stuck on it for last 2 hours.
- (IBAction)addBirthday:(id)sender {
NSString *name = self.textField.text;
NSDate *date = [self.datePicker date];
EKEventStore *eventStore = [[EKEventStore alloc] init];
[eventStore calendarsForEntityType:EKEntityTypeEvent];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = [NSString stringWithFormat:#"%#’s Birthday", name];
event.startDate = date;
event.endDate = date;
NSArray *calendars = [eventStore
calendarsForEntityType:EKEntityTypeEvent];
NSLog(#"ARRAY: %#", [calendars objectAtIndex:0]);
[event setCalendar: [calendars objectAtIndex:0]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
}

The birthdays calendar is likely read only

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

EKEvent alarm AlertView disappears in less then a second

Event is successfully added with alarm that plays ten seconds (for testing purposes) before event start time. Problem is that alarm triggers (UIAlertView on iPhone shows) but it dissapears in less then a second. Very strange...
What could be the problem?
Code:
- (IBAction)addEvent:(id)sender {
EKEventStore *eventStore = [[EKEventStore alloc] init];
if ([eventStore respondsToSelector:#selector(requestAccessToEntityType:completion:)])
{
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
{
EKEvent *anEvent = [EKEvent eventWithEventStore:eventStore];
anEvent.calendar = eventStore.defaultCalendarForNewEvents;
anEvent.title = self.eventNameTextField.text;
NSDate *date = self.datePicker.date;
anEvent.startDate = date;
anEvent.endDate = date;
NSMutableArray *myAlarmsArray = [[NSMutableArray alloc] init];
EKAlarm *alarm1 = [EKAlarm alarmWithRelativeOffset:-10];
[myAlarmsArray addObject:alarm1];
anEvent.alarms = myAlarmsArray;
NSError *eventError;
[eventStore saveEvent:anEvent span:EKSpanThisEvent error:&eventError];
}];
}
}
see the following code hope it will help you.
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = [NSString stringWithFormat:#"%#'s Birthday",strName];
NSDate *twoYearsFromNow = [NSDate dateWithTimeIntervalSinceNow:1577846275];
double alarmAmountInSeconds = 60*60*9;
EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:(1.0*alarmAmountInSeconds)];
EKRecurrenceRule *recurrance;
recurrance = [[EKRecurrenceRule alloc] initRecurrenceWithFrequency:EKRecurrenceFrequencyYearly interval:1 end:[EKRecurrenceEnd recurrenceEndWithEndDate:twoYearsFromNow]];
NSMutableArray *arrayReccurence = [NSMutableArray arrayWithObject:recurrance];
event.recurrenceRules = arrayReccurence;
event.startDate = startDate;
event.endDate = [[NSDate alloc] initWithTimeInterval:600 sinceDate:event.startDate];
event.allDay = true;
event.alarms = [NSArray arrayWithObject:alarm];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];

How to store event in calender in iphone sdk?

EKEventStore *eventDB = [[EKEventStore alloc] init];
EKEvent *myEvent = [EKEvent eventWithEventStore:eventDB];
NSString *trimmedString = [[arrDateList objectAtIndex:i] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
myEvent.title = [d valueForKey:#"garden_name"];
myEvent.startDate = [dateFormatter dateFromString:trimmedString];
myEvent.endDate = [dateFormatter dateFromString:trimmedString];
myEvent.allDay = TRUE;
[myEvent setCalendar:[eventDB defaultCalendarForNewEvents]];
NSError *err;
[eventDB saveEvent:myEvent span:EKSpanThisEvent error:&err];
I am using the above line of code but i cant store the event in calender...
please help me where i have mistake in line of code....
Thanks & regards
Rahul Virja
you can try this code..
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm a"];
NSDate *date = [dateFormatter dateFromString:[taskDetails taskStartDateTime]];
if DEBUG
NSLog(#"here is the task start %#",[taskDetails taskStartDateTime]);
endif
NSDate *end = [dateFormatter dateFromString:[taskDetails taskEndDateTime]];
[dateFormatter release];
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = [taskDetails taskGottaDo];
event.startDate = date;
event.endDate = end;
EKAlarm *reminder = [EKAlarm alarmWithRelativeOffset:-6*60*60];
[event addAlarm:reminder];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSTimeInterval alarmOffset = -1*60;//1 hour
EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:alarmOffset];
[event addAlarm:alarm];
[eventStore release];
NSError *err;
BOOL saved = [eventStore saveEvent:event span:EKSpanThisEvent error:&err];
// NSLog(#"here is the error %#",[eventStore saveEvent:event span:EKSpanThisEvent error:&err]);
if (saved == YES)
{
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#""
message:#"Saved to calendar"
delegate:nil
cancelButtonTitle:#"Right On!" otherButtonTitles:nil] ;
[alertView show];
[alertView release];
}

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.