set my own message in the alertView of calendar events - iphone

I'm currently using the EKEvent and EKEevntUI frameworks,
My Question is : is it possible to set my own message in the UIAlertView for calendar events??
Thanks,

//set event reminder
//make sure to import EventKit framework
EKEventStore *eventDB = [[[EKEventStore alloc] init]autorelease];
EKEvent *myEvent = [EKEvent eventWithEventStore:eventDB];
NSString * eventTitle = #"This is your custom title";
myEvent.title = eventTitle;
//set event time frame (1 hour) the "initWithTimeInterval" is where we account for the users timezone by adding the correctedTime from GMT to the calendar time ( so its not off by hours when entering into calendar)
myEvent.startDate = [[[NSDate alloc] initWithTimeInterval:correctedTimeForCalendarEvent sinceDate:destinationDate ]autorelease];
myEvent.endDate = [[[NSDate alloc] initWithTimeInterval:3600 sinceDate:myEvent.startDate]autorelease];
myEvent.allDay = NO;
//set event reminders 1 day and 1 hour before
myAlarmsArray = [[[NSMutableArray alloc] init] autorelease];
EKAlarm *alarm1 = [EKAlarm alarmWithRelativeOffset:-3600]; // 1 Hour
EKAlarm *alarm2 = [EKAlarm alarmWithRelativeOffset:-86400]; // 1 Day
[myAlarmsArray addObject:alarm1];
[myAlarmsArray addObject:alarm2];
myEvent.alarms = myAlarmsArray;
[myEvent setCalendar:[eventDB defaultCalendarForNewEvents]];
NSError *err;
[eventDB saveEvent:myEvent span:EKSpanThisEvent error:&err];
if (err == noErr) {
//no error, but do not show alert because we do that below.
}

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 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];

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];
}

Manage iPhone Calendar event from our application

The following is my code
NSLog(#"%#", thisEvent1.title);
EKEvent *thisEvent = [EKEvent eventWithEventStore:eventStore];
eventStore = [[EKEventStore alloc] init];
thisEvent = [EKEvent eventWithEventStore:eventStore];
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd:HH:mm"];
NSDate * date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:[itsStartDate objectAtIndex:indexPath.row]];
[date retain];
thisEvent.startDate = [dateFormatter dateFromString:[itsStartDate objectAtIndex:indexPath.row]];
thisEvent.endDate = [dateFormatter dateFromString:[itsEndDate objectAtIndex:indexPath.row]];
thisEvent.notes = [itsNotes objectAtIndex:indexPath.row];
thisEvent.title = [itsTitle objectAtIndex:indexPath.row];
thisEvent.location = [itsLocation objectAtIndex:indexPath.row];
// thisEvent.allDay = TRUE;
NSMutableArray *myAlarmsArray = [[NSMutableArray alloc] init];
EKAlarm *alarm1 = [EKAlarm alarmWithRelativeOffset:-[[itsAlertOne objectAtIndex:indexPath.row] intValue]]; // 1 Hour
// EKAlarm *alarm2 = [EKAlarm alarmWithRelativeOffset:-86400]; // 1 Day
[myAlarmsArray addObject:alarm1];
//[myAlarmsArray addObject:alarm2];
thisEvent.alarms = myAlarmsArray;
[myAlarmsArray release];
//setting the Reuccurence rule
NSString * test1 = [itsRecurrenceFrequency objectAtIndex:indexPath.row];
BOOL isRecurrenceFrequencyExists = TRUE;
EKRecurrenceFrequency recurrenceFrequency;
if ([test1 isEqualToString: #"EKRecurrenceFrequencyDaily"]) {
recurrenceFrequency = EKRecurrenceFrequencyDaily;
}else if([test1 isEqualToString: #"EKRecurrenceFrequencyWeekly"]){
recurrenceFrequency = EKRecurrenceFrequencyWeekly;
}else if([test1 isEqualToString: #"EKRecurrenceFrequencyMonthly"]){
recurrenceFrequency = EKRecurrenceFrequencyMonthly;
}else if([test1 isEqualToString: #"EKRecurrenceFrequencyYearly"]){
recurrenceFrequency = EKRecurrenceFrequencyYearly;
}else{
isRecurrenceFrequencyExists = FALSE;
}
if(isRecurrenceFrequencyExists){
EKRecurrenceRule * recurrenceRule = [[EKRecurrenceRule alloc]
initRecurrenceWithFrequency:recurrenceFrequency
interval:[[itsRecurrenceInterval objectAtIndex:indexPath.row]intValue]
end:nil];
if (thisEvent.endDate != nil) {
EKRecurrenceEnd * end = [EKRecurrenceEnd recurrenceEndWithEndDate:thisEvent.endDate];
recurrenceRule.recurrenceEnd = end;
}else {
thisEvent.endDate = thisEvent.startDate;
}
thisEvent.recurrenceRule = recurrenceRule;
[recurrenceRule release];
}
[thisEvent setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:thisEvent span:EKSpanFutureEvents error:&err];
NSLog(#"%#", thisEvent.eventIdentifier);
[self.eventsList addObject:thisEvent];
In this code I have the Event id of my calendar event.
Now I want to update the event with the changes however Its not updating the previously created event.
Second I Need to know is it possible to capture the changes in calendar event they made in iPhone Calendar including deleting an event.
Can we delete the calendar event using the eventid?
Please help me if any one know the answer.. Thank you in advance.
Regards,
Dilip Rajkumar
Event can be created by this:
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
NSString* str = [[NSString alloc] initWithFormat:#"%#", event.eventIdentifier];
[arrayofCalIDs addObject:str];
You can delete the event using this:
EKEventStore* store = [[[EKEventStore alloc] init] autorelease];
EKEvent* event2 = [store eventWithIdentifier:[arrayofCalIDs objectAtIndex:i]];
if (event2 != nil) {
NSError* error = nil;
[store removeEvent:event2 span:EKSpanThisEvent error:&error];
}
[myPath release];
For Updating Event you cannot directly access any method as it is not available in iOS. So, you can do one thing for this.
(1) First remove the event with eventID.
(2) Create new Event using the same information of the last deleted event.