I have used following code for creating calendar and adding event in that calendar for my application. Everything works fine in the simulator but the calendar is not created in the device. In privacy it does show that my application has the access of the user calendar.
Can anyone please let me know what am I missing?
Thanks in advance.
// this is added in view did load for access
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
// handle access here
}];
//add event to main calendar
EKEventStore *eventStore = [[EKEventStore alloc] init];
// [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
// handle access here
// }];
EKSource *localSource = nil;
for (EKSource *source in eventStore.sources)
if (source.sourceType == EKSourceTypeLocal)
{
localSource = source;
break;
}
EKCalendar *cal;
NSUserDefaults *calid = [NSUserDefaults standardUserDefaults];
if(![calid valueForKey:#"calid"])
{
//cal = [EKCalendar calendarWithEventStore:eventStore];
cal = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:eventStore];
cal.title = #"my calendar title";
cal.source = localSource;
[eventStore saveCalendar:cal commit:YES error:nil];
NSString *strid= [NSString stringWithFormat:#"%#",cal.calendarIdentifier];
[calid setObject:strid forKey:#"calid"];
NSLog(#"cal id = %#", cal.calendarIdentifier);
}
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = myTextField.text;
event.startDate = SelectedDate;
event.endDate = [[NSDate alloc] initWithTimeInterval:3600 sinceDate:event.startDate];
//[event setCalendar:[eventStore defaultCalendarForNewEvents]];
[event setCalendar:[eventStore calendarWithIdentifier:[calid valueForKey:#"calid"]]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
NSLog(#"%#",err);
Related
I am new in iOS. and I created Google Calendar with the help of this link https://developers.google.com/google-apps/calendar/quickstart/ios.
It fetch the event which is created in Google Calendar.But I need to show Google Calendar in my App. Thanks in Advance .
For Event I used a code
EKEventStore *store = [EKEventStore new];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted) { return; }
EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = #"Birthday XYZ";
event.startDate = [NSDate date]; //today
event.endDate = [event.startDate dateByAddingTimeInterval:60*60]; //set 1 hour meeting
event.calendar = [store defaultCalendarForNewEvents];
NSError *err = nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
// self.savedEventId = event.eventIdentifier; //save the event id if you want to access this later
}];
But this code is not create event.
In my iOS app I want to create event in calendar and I've found code but the code directly creates an event instead of opening Add event screen. I want to allow user to set reminder through add event screen.
My Code is Below :
EKEventStore *es = [[EKEventStore alloc] init];
EKAuthorizationStatus authorizationStatus = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];
BOOL needsToRequestAccessToEventStore = (authorizationStatus == EKAuthorizationStatusNotDetermined);
if (needsToRequestAccessToEventStore) {
[es requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (granted) {
EKEvent *event = [EKEvent eventWithEventStore:es];
event.title = #"Event Title";
event.startDate = [NSDate date]; // today
event.endDate = [event.startDate dateByAddingTimeInterval:60*60]; // Duration 1 hr
[event setCalendar:[es defaultCalendarForNewEvents]];
NSError *err = nil;
[es saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
NSLog(#"Error : %#", err);
} else {
// Denied
}
}];
} else {
BOOL granted = (authorizationStatus == EKAuthorizationStatusAuthorized);
if (granted) {
EKEvent *event = [EKEvent eventWithEventStore:es];
event.title = #"Event Title";
event.startDate = [NSDate date]; // today
event.endDate = [[NSDate date] dateByAddingTimeInterval:60*60]; // Duration 1 hr
[event setCalendar:[es defaultCalendarForNewEvents]];
NSError *err = nil;
[es saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
NSLog(#"Error : %#", err);
} else {
// Denied
}
}
Based in the EKEventStore documentation, the method: [es saveEvent:event span:EKSpanThisEvent commit:YES error:&err]; is saving the event. Your code is working correctly and this code should not open the "Add event screen" as you are expecting.
I am trying to add an event to custom calendar.
To fetch the calendar list I used below
EKEventStore *eventStore = [[EKEventStore alloc] init];
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if(!granted) {
//error alert
return;
}
NSArray *calendars = [eventStore calendarsForEntityType:EKEntityTypeEvent];
// List of all calendars
// Let user to choose a calendar.
}];
For add an event I used the below code
-(void)addEvent:(EventStoreClass *)storeObject
{
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted) return;
EKEvent *event = [EKEvent eventWithEventStore:store];
[event setTitle:[storeObject eventTitle]];
[event setStartDate:[storeObject eventStartDate]];
[event setEndDate:[storeObject eventEndDate]];
[event setCalendar:[storeObject calendar] ? [storeObject calendar] : [store defaultCalendarForNewEvents]];
[store saveEvent:event span:EKSpanThisEvent commit:YES error:nil];
}];
}
Now when I used the default calendar it's successfully added but when I choose any other calendar the application get crashed and in log it's shows
*** -[EKSource retain]: message sent to deallocated instance 0x7f9bddb1bf50
I am not able to find out the exact problem or for which line this problem arise. Any advice will be appreciable.
You have to check whether that particular calendar allows you to modify it or not
if ([storeObject calendar].allowsContentModifications)
{
//add event now
}
Yes. Your store object is deallocated before completion block starts to run.
The easiest way to fix it - save store object as a field:
EKEventStore *store;
-(void)addEvent:(EventStoreClass *)storeObject
{
store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted) return;
EKEvent *event = [EKEvent eventWithEventStore:store];
[event setTitle:[storeObject eventTitle]];
[event setStartDate:[storeObject eventStartDate]];
[event setEndDate:[storeObject eventEndDate]];
[event setCalendar:[storeObject calendar] ? [storeObject calendar] : [store defaultCalendarForNewEvents]];
[store saveEvent:event span:EKSpanThisEvent commit:YES error:nil];
}];
}
This problem was solved by doing the below.
-(void)addEvent:(EventStoreClass *)storeObject
{
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted) return;
EKEvent *event = [EKEvent eventWithEventStore:store];
[event setTitle:[storeObject eventTitle]];
[event setStartDate:[storeObject eventStartDate]];
[event setEndDate:[storeObject eventEndDate]];
if([storeObject calendar]) {
NSArray *calendars = [store calendarsForEntityType:EKEntityTypeEvent];
__block EKCalendar *calendar;
[calendars enumerateObjectsUsingBlock:^(EKCalendar *cal, NSUInteger idx, BOOL *stop) {
if([[cal title] isEqualToString:[[storeObject calendar] title]]) {
calendar = cal;
*stop = YES;
}
}];
[event setCalendar:calendar];
}
else {
[event setCalendar:[store defaultCalendarForNewEvents]];
}
[store saveEvent:event span:EKSpanThisEvent commit:YES error:nil];
}];
}
The problem was on [storeObject calendar] as this calendar was taken from another store and we are assigning it with different store. So I just took the calendars from the same store and it's work.
I have saved a reminder using EKReminder,now I want to remove this reminder from EKReminder.
I have date and time of the reminder to be removed. I tried using NSPRedicate but it didn't work for me.
I am using this code:
EKEventStore *eventStore = [[EKEventStore alloc] init] ;
EKReminder *reminder = [EKReminder
reminderWithEventStore:eventStore];
reminder.calendar = [eventStore defaultCalendarForNewReminders];
EKAlarm *reminderDate = [EKAlarm alarmWithAbsoluteDate:removeReminder];
[reminder removeAlarm:reminderDate];
NSError *error = nil;
[eventStore removeReminder:reminder commit:YES error:&error];
if (error)
{
NSLog(#"error = %#", error);
}
else{
NSLog(#"Reminder removed successfully");
}
Where removeReminder is my date.
Please suggest me if there is problem with this code.
I found my answer:
EKEventStore *eventStore = [[EKEventStore alloc] init] ;
NSArray *calendarArray = [NSArray arrayWithObject:[eventStore defaultCalendarForNewReminders]];
NSPredicate *predicate = [eventStore predicateForRemindersInCalendars:calendarArray];
[eventStore fetchRemindersMatchingPredicate:predicate completion:^(NSArray *reminders)
{
for (EKReminder *reminder in reminders)
{
//here based on reminder.title i removed the reminder.
[eventStore removeReminder:reminder commit:YES error:nil];
}
Good day,
I can't add custom event in standard iOS Calendar that will be repeated (during for example 2 months).
The problem is that my event is just up to the end of current month. It doesn't matter which day it will start - it will be repeating only during this month.
Code:
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
{
if (!granted)
return;
// setting the values directly for testing purpose.
EKRecurrenceEnd *endRecurrence = nil;
NSMutableArray *daysOfTheWeekArray = [NSMutableArray array];
//Only weekends
[daysOfTheWeekArray addObject:[EKRecurrenceDayOfWeek dayOfWeek:6]];
[daysOfTheWeekArray addObject:[EKRecurrenceDayOfWeek dayOfWeek:7]];
endRecurrence = [EKRecurrenceEnd recurrenceEndWithEndDate:[[NSDate date] dateByAddingTimeInterval:60*60*24*7*10]]];
//create the recurrence rule here
EKRecurrenceRule *recurrence = [[EKRecurrenceRule alloc] initRecurrenceWithFrequency: EKRecurrenceFrequencyYearly
interval:1
daysOfTheWeek:daysOfTheWeekArray
daysOfTheMonth:nil
monthsOfTheYear:nil
weeksOfTheYear:nil
daysOfTheYear:nil
setPositions:nil
end:endRecurrence];
EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = #"Title";
event.startDate = [NSDate date]; //today
event.endDate = [event.startDate dateByAddingTimeInterval:60*60]; //set 1 hour meeting
[event addRecurrenceRule: recurrence];
[event setCalendar:[store defaultCalendarForNewEvents]];
NSError *err = nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
}];
Please change your frequency with EKRecurrenceFrequencyMonthly, currently it's EKRecurrenceFrequencyYearly:
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
{
if (!granted)
return;
// setting the values directly for testing purpose.
EKRecurrenceEnd *endRecurrence = nil;
NSMutableArray *daysOfTheWeekArray = [NSMutableArray array];
//Only weekends
[daysOfTheWeekArray addObject:[EKRecurrenceDayOfWeek dayOfWeek:6]];
[daysOfTheWeekArray addObject:[EKRecurrenceDayOfWeek dayOfWeek:7]];
endRecurrence = [EKRecurrenceEnd recurrenceEndWithEndDate:[[NSDate date] dateByAddingTimeInterval:60*60*24*7*10]]];
//create the recurrence rule here
EKRecurrenceRule *recurrence = [[EKRecurrenceRule alloc] initRecurrenceWithFrequency: EKRecurrenceFrequencyMonthly
interval:1
daysOfTheWeek:daysOfTheWeekArray
daysOfTheMonth:nil
monthsOfTheYear:nil
weeksOfTheYear:nil
daysOfTheYear:nil
setPositions:nil
end:endRecurrence];
EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = #"Title";
event.startDate = [NSDate date]; //today
event.endDate = [event.startDate dateByAddingTimeInterval:60*60]; //set 1 hour meeting
[event addRecurrenceRule: recurrence];
[event setCalendar:[store defaultCalendarForNewEvents]];
NSError *err = nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
}];