adding event does not show up in iPhone calendar - iphone

Trying programistically add an event to iPhone calendar.
I get no errors, however, nothing gets added to the calendar either.
savedEventId(below) returns (null)
here is the code:
Methods
Adding event :
if(accessGranted){
EKEvent *event = [EKEvent eventWithEventStore:store];
event.startDate =[NSDate date];
event.endDate = [event.startDate dateByAddingTimeInterval:60*60];
[event setCalendar:[store defaultCalendarForNewEvents]];
NSError *err = nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
NSString *savedEventId = event.eventIdentifier;
}
getting access to calendar
in viewDidLoad:
store = [[EKEventStore alloc] init];
accessGranted=FALSE;
[self.store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
accessGranted=TRUE;
}];
in .h file
#import <EventKit/EventKit.h>
#interface
EKEventStore *store;
BOOL accessGranted;
#property(nonatomic, retain) EKEventStore *store;
in .m file
#synthesize store;

Try this code instead:
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted) { return; }
EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = #"Event Title";
event.startDate = [NSDate date]; //today
event.endDate = [event.startDate dateByAddingTimeInterval:60*60]; //set 1 hour meeting
[event setCalendar:[store defaultCalendarForNewEvents]];
NSError *err = nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
NSString *savedEventId = event.eventIdentifier; //this is so you can access this event later
}];
Remember to:
request access to the user's calendar via "requestAccessToEntityType:completion:"
commit your event

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.

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

Add event to Birthday calendar in iOS

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

Add Event to calendar in xcode iOS

Hy
I have this code for adding Events to calendar but it does not add.
-(void)event
{
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = #"Event";
NSDateFormatter *tempFormatter = [[NSDateFormatter alloc]init];
[tempFormatter setDateFormat:#"dd.MM.yyyy HH:mm"];
NSString *dateandtime =[NSString stringWithFormat:#"%#%#%#",datestring,#" ",starttimestring];
NSString *dateandtimeend =[NSString stringWithFormat:#"%#%#%#",datestring,#" ",endtimestring];
event.startDate = [tempFormatter dateFromString:dateandtime];
event.endDate = [tempFormatter dateFromString:dateandtimeend];
[event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -60.0f * 24]];
[event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -15.0f]];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
}
From the XML I get the date and time in this format:
datestring: 28.10.2012
starttimestring: 15:00
Are you on the iOS 6 simulator or on a device with iOS 6? If so, you need to ask the user for permission to use the event store before you can save items to it.
Basically, if the requestAccessToEntityType:completion: selector is available on your event store object, you call that method and provide a block of code that is executed when the user grants permission, and you would then do your event saving in that block.
First add the EventKit framework to your project and don't forget to include the import:
#import <EventKit/EventKit.h>
Here is a code snippet that I used that worked for me:
EKEventStore *eventStore = [[EKEventStore alloc] init];
if ([eventStore respondsToSelector:#selector(requestAccessToEntityType:completion:)])
{
// the selector is available, so we must be on iOS 6 or newer
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error)
{
// display error message here
}
else if (!granted)
{
// display access denied error message here
}
else
{
// access granted
// ***** do the important stuff here *****
}
});
}];
}
else
{
// this code runs in iOS 4 or iOS 5
// ***** do the important stuff here *****
}
[eventStore release];
Here is a blog post that I did on this subject:
http://www.dosomethinghere.com/2012/10/08/ios-6-calendar-and-address-book-issues/
1) add Eventkit framework and #import <EventKit/EventKit.h>
2)
-(void)syncWithCalendar {
EKEventStore *store = [EKEventStore new];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted) { return; }
EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = #"Event Title Testing"; //give event title you want
event.startDate = [NSDate date];
event.endDate = [event.startDate dateByAddingTimeInterval:60*60];
event.calendar = [store defaultCalendarForNewEvents];
NSError *err = nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
}];
}
3) call function
[self syncWithCalendar];

Removing events from iPhone calendar with EKEventStore

I'm trying to remove events that i have created from the iPhone calendar.
I tried this, but it always returns NO:
[eventStore removeEvent:event span:EKSpanThisEvent error:&err];
I created the event as follows and it works:
eventStore = [[EKEventStore alloc] init];
event = [EKEvent eventWithEventStore:eventStore];
event.title = #"EVENT TITLE";
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd:HH:mm"];
NSDate * date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:#"2010-8-15:12:30"];
[date retain];
event.startDate = date;
event.endDate = [[NSDate alloc] initWithTimeInterval:600 sinceDate:event.startDate];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
Is there a way to remove this event? Or it would be be better that if I try to write this event again it only modifies it instead of creating a new one.
Thanks,
After creating the event I save the eventIdentifier in an array:
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
NSString* str = [[NSString alloc] initWithFormat:#"%#", event.eventIdentifier];
[arrayofCalIDs addObject:str];
To delete the events:
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];
Just an FYI for the answer above. It is found on the web on with this link:
http://tech.vniup.com/index.php/iphone/objective-c/how-to-delete-event-from-iphone-calendar-programmatically.html
My only suggestion is that if you are building an array of objects each object ideally would be the event. Then do a reverse array operation because the latest event will always be at the bottom.
NSMutableArray *reverseArray = [NSMutableArray arrayWithCapacity:[eventsList count]];
for (id element in [eventsList reverseObjectEnumerator]) {
[reverseArray addObject:element];
}
eventsList = reverseArray;
And also in the display of the events be nice to your users and display the start date of the event.
Anyway, after you have an array objects that are EKEvents you can do this which is MUCH easier.
EKEvent *eventToRemove = [myEventStore eventWithIdentifier:thisEvent.eventIdentifier ];
if ([eventToRemove.eventIdentifier length] > 0) {
NSError* error = nil;
[myEventStore removeEvent:eventToRemove span:EKSpanThisEvent error:&error];
}
Then you can remove that same event from you array of events for the table display....easy!