Add event screen is not opening in iOS - 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.

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

iCal Eventstore - successful function on completion

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.

What format to enter an NSDate in

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

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