im trying to create an event from my app, to add it to the calendar, im using the code below:
- (IBAction)refreshTapped:(id)sender {
// [self performSegueWithIdentifier:#"calendarsegue" sender:nil];
EKEventStore *store = [[EKEventStore alloc] init];
if([store respondsToSelector:#selector(requestAccessToEntityType:completion:)])
{
// iOS 6
[store requestAccessToEntityType:EKEntityTypeEvent
completion:^(BOOL granted, NSError *error) {
if (granted)
{
dispatch_async(dispatch_get_main_queue(), ^{
[self createEventAndPresentViewController:store];
});
}
}];
} else
{
// iOS 5
[self createEventAndPresentViewController:store];
}
}
- (void)createEventAndPresentViewController:(EKEventStore *)store
{
EKEvent *event = [self findOrCreateEvent:store];
EKEventEditViewController *controller = [[EKEventEditViewController alloc] init];
controller.event = event;
controller.eventStore = store;
controller.editViewDelegate = self;
[self presentViewController:controller animated:YES completion:nil];
}
- (EKEvent *)findOrCreateEvent:(EKEventStore *)store
{
NSString *title = #"My event title";
// try to find an event
EKEvent *event = [self findEventWithTitle:title inEventStore:store];
// if found, use it
if (event)
return event;
// if not, let's create new event
event = [EKEvent eventWithEventStore:store];
event.title = title;
event.notes = #"";
// event.location = #"Lebanon";
event.calendar = [store defaultCalendarForNewEvents];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
components.hour = 4;
event.startDate = [calendar dateByAddingComponents:components
toDate:[NSDate date]
options:0];
components.hour = 1;
event.endDate = [calendar dateByAddingComponents:components
toDate:event.startDate
options:0];
return event;
}
- (EKEvent *)findEventWithTitle:(NSString *)title inEventStore:(EKEventStore *)store
{
// Get the appropriate calendar
NSCalendar *calendar = [NSCalendar currentCalendar];
// Create the start range date components
NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init];
oneDayAgoComponents.day = -1;
NSDate *oneDayAgo = [calendar dateByAddingComponents:oneDayAgoComponents
toDate:[NSDate date]
options:0];
// Create the end range date components
NSDateComponents *oneWeekFromNowComponents = [[NSDateComponents alloc] init];
oneWeekFromNowComponents.day = 7;
NSDate *oneWeekFromNow = [calendar dateByAddingComponents:oneWeekFromNowComponents
toDate:[NSDate date]
options:0];
// Create the predicate from the event store's instance method
NSPredicate *predicate = [store predicateForEventsWithStartDate:oneDayAgo
endDate:oneWeekFromNow
calendars:nil];
// Fetch all events that match the predicate
NSArray *events = [store eventsMatchingPredicate:predicate];
for (EKEvent *event in events)
{
if ([title isEqualToString:event.title])
{
return event;
}
}
return nil;
}
- (void)eventEditViewController:(EKEventEditViewController *)controller didCompleteWithAction:(EKEventEditViewAction)action
{
[self dismissViewControllerAnimated:YES completion:nil];
// [self fetchCalendarEvents];
}
the event page is opening normally, but when i want to enter the location by keyboard, the app is crashing and giving me the error above in the title enter image description here
Anyone knows where the error is?thanks
Related
I believe this worked before but now when I add a event to my calendar when I am in time zone set to automatically or Louisville which is EDT, and I add an event that is EDT it adds it to my calendar a hour ahead.
I do this on another app that has events in CST, and when I add them to my calendar, it adds them just fine. Is my code wrong or something?
- (void)viewWillAppear:(BOOL)animated {
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:self.event.timeZone]];
[dateFormat setDateFormat:#"yyyy-MM-ddHH:mm:ss"];
self.startDate = [dateFormat dateFromString:self.game.dateTime];
self.endDate = [[NSDate alloc] initWithTimeInterval:60*self.event.gameDuration sinceDate:self.startDate];
}
- (void) addToCalendar
{
EKEventStore *eventStore = [[EKEventStore alloc] init];
if ([eventStore respondsToSelector:#selector(requestAccessToEntityType:completion:)]){
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
{
for (EKSource *source in eventStore.sources)
{
if (source.sourceType == EKSourceTypeCalDAV || source.sourceType == EKSourceTypeLocal)
{
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = [NSString stringWithFormat:#"%# vs. %#", self.game.awayTeam.team.name, self.game.homeTeam.team.name];
event.location = [NSString stringWithFormat:#"%# - %#", self.game.venue.name, self.game.venue.court];
event.startDate = self.startDate;
event.endDate = self.endDate;
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
if(err) {
dispatch_async(dispatch_get_main_queue(), ^{
[self showAlert:#"Error" desc:#"There was a problem adding this game to your calendar"];
});
}
else {
dispatch_async(dispatch_get_main_queue(), ^{
[self showAlert:#"Success" desc:#"Game added to your calendar"];
});
}
break;
}
}
}];
}
}
I found out Apple uses their own timezone strings so I just did a translation from Windows Timezones to Apple when sending it via our API.
In my application i am adding an event to devices calendar.I am doing like this
if ([db.saveCalenderSettings isEqualToNumber:[NSNumber numberWithBool:YES]]){
NSPredicate *predicateForEventOnDate = [store predicateForEventsWithStartDate:strtdate endDate:[strtdate dateByAddingTimeInterval:60*60] calendars:nil];
NSArray *eventsOnDate = [store eventsMatchingPredicate:predicateForEventOnDate];
__block BOOL eventExists = NO;
[eventsOnDate enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
EKEvent *eventToCheck = (EKEvent*)obj;
if([note isEqualToString:eventToCheck.notes]) {
eventExists = YES;
*stop = YES;
}
}];
NSLog(#"%#",eventsOnDate);
if(! eventExists) {
NSLog(#"Event not exists so adding");
EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = subject; //give event title you want
event.notes = note ;
event.startDate = strtdate;
event.endDate = [event.startDate dateByAddingTimeInterval:60*60];
event.calendar = [store defaultCalendarForNewEvents];
// EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:60*15*-1];
// event.alarms = [NSArray arrayWithObject:alarm];
NSError *err = nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
if (!err) {
[db setSavedtocalender:#"yes"];
}
else {
// NSLog(#" Event not created");
[db setSavedtocalender:#"no"];
}
}
else {
NSLog(#"Event exists");
[db setSavedtocalender:#"yes"];
}
}
But the problem is the same event gets added multiple times in the default calendar .Can anybody tells me where i am going wrong?
Hi For each and every event one id is alloted.
Please Check is the event is added or not.If event added then store event id and check agin before saving event again.
For getting Event Id :
//save the event id if you want to access this later
NSString *eventId=event.eventIdentifier;
For Future Refernce :
// To add event in Calender App
EKEventStore *store = [EKEventStore new];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted) { return; }
EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = #"To meet Chandu.";
event.startDate = [NSDate date]; //today
event.endDate = [event.startDate dateByAddingTimeInterval:60*60]; //set 1 hour meeting
event.calendar = [store defaultCalendarForNewEvents];
event.location=#"Sandriver Technologies";
NSError *err = nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
NSLog(#"%#",event.eventIdentifier); //save the event id if you want to access this later
eventId=event.eventIdentifier;
}];
//to remove event from app
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted) { return; }
EKEvent* eventToRemove = [store eventWithIdentifier:eventId];
if (eventToRemove) {
NSError* error = nil;
[store removeEvent:eventToRemove span:EKSpanThisEvent commit:YES error:&error];
}
}];
// Note: Get all event list
//To get Appropriate calender
NSCalendar *calendar = [NSCalendar currentCalendar];
if ([store respondsToSelector:#selector(requestAccessToEntityType:completion:)])
{
/* iOS Settings > Privacy > Calendars > MY APP > ENABLE | DISABLE */
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
{
if ( granted )
{
NSLog(#"User has granted permission!");
// Create the start date components
NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init];
oneDayAgoComponents.day = -1;
NSDate *oneDayAgo = [calendar dateByAddingComponents:oneDayAgoComponents
toDate:[NSDate date]
options:0];
// Create the end date components
NSDateComponents *oneYearFromNowComponents = [[NSDateComponents alloc] init];
oneYearFromNowComponents.year = 1;
NSDate *oneYearFromNow = [calendar dateByAddingComponents:oneYearFromNowComponents
toDate:[NSDate date]
options:0];
// Create the predicate from the event store's instance method
NSPredicate *predicate = [store predicateForEventsWithStartDate:oneDayAgo
endDate:oneYearFromNow
calendars:nil];
// Fetch all events that match the predicate
NSArray *events = [store eventsMatchingPredicate:predicate];
NSLog(#"The content of array is%#",events);
}
else
{
NSLog(#"User has not granted permission!");
}
}];
}
I use this code below to add event , my code called many times so i have duplicate event , is there any idea to prevent duplicate
Thank You in advance
EKEvent *event = [EKEvent eventWithEventStore:es];
NSDateFormatter *dateFormats = [[NSDateFormatter alloc]init];
[dateFormats setDateFormat:#"yyy-MM-dd"];
NSDate *date1 = [[NSDate alloc] init];
NSDate *date2 = [[NSDate alloc] init];
date1 = nil;
date2 = nil;
date1 = [dateFormats dateFromString:event2.from_date];
date2 = [dateFormats dateFromString:event2.to_date];
event.title = event2.event_title;
event.allDay = NO;
event.startDate = date1;
event.endDate = date2;
[event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f *60.0f *24]];
//put here if start and end dates are same
[event setCalendar:[es defaultCalendarForNewEvents]];
[es saveEvent:event span:EKSpanThisEvent commit:YES error:nil];
I'm basing my answer off this tutorial Calendar event management tutorial. So of course if things don't line up correctly, please reference it.
EKEvent *event = [EKEvent eventWithEventStore:es];
NSDate *date1 = [NSDate new];
NSDate *date2 = [NSDate new];
date1 = nil;
date2 = nil;
date1 = [dateFormats dateFromString:event2.from_date];
date2 = [dateFormats dateFromString:event2.to_date];
event.title = event2.event_title;
event.allDay = NO;
event.startDate = date1;
event.endDate = date2;
NSPredicate *predicate = [es predicateForEventsWithStartDate:event2.from_date endDate:event2.to_date calendars:nil];
NSArray *eventsOnDate = [es eventsMatchingPredicate:predicate];
__block BOOL eventExists = NO;
[eventsOnDate enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
EKEvent *eventToCheck = (EKEvent*)obj;
if([event.title isEqualToString:eventToCheck.title])
{
eventExists = YES;
*stop = YES;
}
}];
if(! eventExists) {
[event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f *60.0f *24]];
//put here if start and end dates are same
[event setCalendar:[es defaultCalendarForNewEvents]];
[es saveEvent:event span:EKSpanThisEvent commit:YES error:nil];
NSError *saveEventError;
[es saveEvent:event span:EKSpanThisEvent error: &saveEventError];
if(saveEventError) {
//ERROR OCCURED WHILE SAVING
} else {
//SUCCESSFULLY SAVED
}
} else {
// EVENT ALREADY EXISTS
}
So I need to get the current event in the calendar. I.E - an event that started and did not end yet. I have written some code but it does not work.
Through debugging I noticed my oneDayAgo variable is nil and I do not understand why.
The oneWeekFromNow variable is good.
Here is the method I have written:
-(void)getCurrentEvent{
// Get appropriate calendar
[self.store requestAccessToEntityType:EKEntityTypeEvent
completion:^(BOOL granted, NSError *error) {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init];
oneDayAgoComponents.day -=1;
NSDate *oneDayAgo = [calendar dateByAddingComponents:oneDayAgoComponents
toDate:[NSDate date]
options:0];
NSDateComponents *oneWeekFromNowComponents = [[NSDateComponents alloc] init];
oneWeekFromNowComponents.week = 1;
NSDate *oneWeekFromNow = [calendar dateByAddingComponents:oneWeekFromNowComponents
toDate:[NSDate date]
options:0];
NSPredicate *predicate = [self.store predicateForEventsWithStartDate:oneDayAgo
endDate:oneWeekFromNow
calendars:nil];
NSMutableArray *currentEvens = [[NSMutableArray alloc]init];
// Fetch all events that match the predicate
[self.store enumerateEventsMatchingPredicate:predicate usingBlock:^(EKEvent *event, BOOL *stop) {
if (([event.startDate compare:[NSDate date]] == NSOrderedDescending) &&
([[NSDate date] compare:event.endDate] == NSOrderedDescending)) {
[currentEvens addObject:event];
}
}];
self.lblEvent.text = [NSString stringWithFormat:#"%#", currentEvens];
[self.view reloadInputViews];
}];
}
Try this instead:
NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init];
oneDayAgoComponents.day = -1;
Here is the corrected code that worked for me. I also needed to modify some other things:
-(void)getCurrentEvent{
// Get appropriate calendar
[self.store requestAccessToEntityType:EKEntityTypeEvent
completion:^(BOOL granted, NSError *error) {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init];
oneDayAgoComponents.day = -1;
NSDate *oneDayAgo = [calendar dateByAddingComponents:oneDayAgoComponents
toDate:[NSDate date]
options:0];
NSDateComponents *oneWeekFromNowComponents = [[NSDateComponents alloc] init];
oneWeekFromNowComponents.week = 1;
NSDate *oneWeekFromNow = [calendar dateByAddingComponents:oneWeekFromNowComponents
toDate:[NSDate date]
options:0];
NSPredicate *predicate = [self.store predicateForEventsWithStartDate:oneDayAgo
endDate:oneWeekFromNow
calendars:nil];
NSMutableArray *currentEvens = [[NSMutableArray alloc]init];
// Fetch all events that match the predicate
[self.store enumerateEventsMatchingPredicate:predicate usingBlock:^(EKEvent *event, BOOL *stop) {
if (([event.startDate compare:[NSDate date]] == NSOrderedAscending) &&
([[NSDate date] compare:event.endDate] == NSOrderedAscending)) {
[currentEvens addObject:event];
}
}];
NSString *currentEventsString = [[NSString alloc]init];
for (EKEvent *event in currentEvens) {
currentEventsString = [currentEventsString stringByAppendingString:event.title];
}
dispatch_async(dispatch_get_main_queue(), ^{
self.lblEvent.text = currentEventsString;
});
}];
}
I am setting a calendar event from code in my app, and setting the date from my date picker. Here is what I have:
-(void)setCalenderEvent
{
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
NSDate *startDate = [[NSDate alloc] init];
NSDate *endDate = [[NSDate alloc] init];
event.title = #"Test from date picker";
// event.startDate = startDate;
// event.endDate = endDate;
NSDate *selectedDate = self.datePickerForReminder.date;
event.startDate = selectedDate;
event.allDay = NO;
event.notes = #"Created by Quick Reminders!!";
/* iOS 6 requires the user grant your application access to the Event Stores */
if ([eventStore respondsToSelector:#selector(requestAccessToEntityType:completion:)])
{
/* iOS Settings > Privacy > Calendars > MY APP > ENABLE | DISABLE */
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
{
if ( granted )
{
NSLog(#"User has granted permission!");
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
if (err == noErr){
NSLog(#"No Error");
}
[startDate release];
[endDate release];
[eventStore release];
}
else
{
NSLog(#"User has not granted permission!");
}
}];
}
}
Can anyone tell me what I am doing wrong?
Thanks!
I was able to get it! You need a span time (add end time).
event.endDate = [selectedDate dateByAddingTimeInterval:30*60];