This is how I am adding an event to Ical I just dont know how to make the start date something other than the current day, as well as set the duration as an all day event.
> EKEventStore *store = [[EKEventStore alloc] init];
> [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
> if (!granted) { return; }
> EKEvent *event = [EKEvent eventWithEventStore:store];
> event.title = #"Hits THermal DC I";
> 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
> }];
To create any date with your own values, do this:
NSCalendar* calendar=[NSCalendar currentCalendar];
NSDateComponents* dateComps=[[NSDateComponents alloc] init];
[dateComps setYear:1990];
[dateComps setMonth:11];
[dateComps setDay:10];
NSDate* starDate=[calendar dateFromComponents:dateComps];
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted) { return; }
EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = #"Hits THermal DC I";
event.startDate = starDate
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
}];
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.
I want to create events of between two dates like that 2015/10/01 to 2015/10/05 create event between two dates
#import <EventKit/EventKit.h>
#import <EventKitUI/EventKitUI.h>
-(void)yourmethodToSaveEvent {
EKEventStore *eventStore = [[EKEventStore alloc] init];
[self checkForEventAuthorization:eventStore completionHandler:^(BOOL granted, NSError *error) {
if (granted) {
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = #"title";
event.startDate = [NSDate date]; // start date
event.endDate = [NSDate date]; // end date ;
event.notes = #"notes";
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
BOOL isSuccess = [eventStore saveEvent:event span:EKSpanThisEvent error:&err];
if(isSuccess) {
NSLog (#"your event saved successfully.");
} else {
NSLog (#"failed.");
}
}
}];
}
-(void)checkForEventAuthorization:(EKEventStore *)eventStore completionHandler:(void (^)(BOOL granted, NSError *error))block {
if ([eventStore respondsToSelector:#selector(requestAccessToEntityType:completion:)]) {
// iOS 6 and later
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
block (granted,error);
}];
} else {
block (YES,nil);
}
}
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.
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];
}];
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);