iCal Eventstore - successful function on completion - ios

I have the following function to add a dynamic date to Ical -
-(void)AddToIcal{
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted) { return; }
EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = self.booked.bookingTitle;
event.startDate = self.booked.bookingDate;
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
}];
}
Does anyone know how I could show a stored / not stored message when the ad event function is complete?

what about changing
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
to
if (![store saveEvent:event span:EKSpanThisEvent commit:YES error:&err]) {
NSLog([NSString stringWithFormat:#"Error saving event: %#", error.localizedDescription]);
} else {
NSLog(#"Successfully saved event.");
}
You could as well do something different than writing to NSLog, like use an UIAlertView or such.
Also you may have a look a the Return Value section of the saveEvent:span:commit:error Apple documentation.
It says:
Return Value
If successful, YES; otherwise, NO. Also returns NO if event does not need to be saved because it has not been modified.

Related

How to add Google Calendar in Objective-C

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.

Create Events between two dates in ios

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

Add event screen is not opening in iOS

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.

Add Event to custom calendar crashed the application

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.

create calendar not working in device - ios sdk

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

Resources