Thursday, November 22, 2012

How To Add Events To Calendar in iOS 6

Key word:   EKEventStore, EventKit Framework, iCal, user permissions, requestAccessToEntityType, Privacy settings, ios6

Get contacts of all your emergency needs on single tap. Download Quick-Finder for iPhone

How To Add Events To Calendar in iOS 6

Get contacts of all your emergency needs on single tap. Download Quick-Finder for iPhone

1) Now Apple make it compulsory to take user permission to add a event to iCal (As we are taking permission for location). So the code which you have written till yet will not work on iOS6.

For iOS 6, You need to take permission from user. For this you have to execute the following code.


 EKEventStore *eventstore = [[EKEventStore alloc] init];
 if([eventstore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) 

[eventstore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) 


if(granted)
{
// if user granted your app to use calendar control will reach over hare
// add the code to add event to iCal that you are using previously for iOS5

    EKEvent *event  = [EKEvent eventWithEventStore: eventstore];
    event.title     = [NSString stringWithStdString:eventName] ;
    event.startDate = [NSDate date];
    [event setCalendar:[eventStore defaultCalendarForNewEvents]];
    NSError *err;
    [eventStore saveEvent:event span:EKSpanThisEvent error:&err];

}
else
{
// if user did not your app to use calendar control will reach over hare
}

}];
}


If user did not allow your app to use calendar you can also suggest him to allow and shows alert massage like this

UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Info" message:@"Pls go to Setting----> Privacy -----> Calendar ----> Choose App Name. " delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

[alert show];



1 comment: