When I try to call [newEventStore defaultCalendarForNewEvents], it returns an error message says:
[707:907] defaultCalendarForNewEvents failed: Error Domain
Ios app wont be able to get any data from the Calendar on the iOS6 system if you don’t call the – requestAccessToEntityType:completion: function to prompt a dialog to ask your users to grant access to your app to access the Calendar/Reminder . Code will look like:
//CalendarEventHandler.h
@interface CalendarEventHandler : NSObject
{
EKEventStore *eventStore;
}
@property (nonatomic, strong) EKEventStore *eventStore;
//CalendarEventHandler.m
self.eventStore =[[EKEventStore alloc] init];
if([self checkIsDeviceVersionHigherThanRequiredVersion:@"6.0"]) {
[self.eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (granted){
//---- codes here when user allow your app to access theirs' calendar.
}else
{
//----- codes here when user NOT allow your app to access the calendar.
}
}];
}else {
//---- codes here for IOS < 6.0.
}
// Below is a block for checking is current ios version higher than required version.
- (BOOL)checkIsDeviceVersionHigherThanRequiredVersion:(NSString *)requiredVersion
{
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:requiredVersion options:NSNumericSearch] != NSOrderedAscending)
{
return YES;
}
return NO;
}
Go to Simulator/Device settings/Privacy/Calendars/YourApp Tap ON to allow calendar access. And retry your code.It will work.
Solved. I accidentally turn off the app access to calendar in Setting->Privacy on IOS6
(based on @yunas answer)
_estore = EKEventStore()
_estore.reset()
_estore.requestAccessToEntityType(EKEntityTypeEvent) { (granted, error) in
if granted {
println("allowed")
/* ... */
} else {
println("not allowed")
}
}
It will open "Access" pop-up
For Xamarin users:
EKEventStore eventStore = new EKEventStore();
eventStore.RequestAccess(EKEntityType.Event, (bool granted, NSError e) =>
{
if (granted)
{
//Your code here when user gief permissions
}
});
On iOS6, Apple introduced a new privacy control that allows the user to control the accessibility of contacts and calenders for each app. So, on the code side, you need to add some way to request the permission. In iOS5 or before, we can always call
EKEventStore *eventStore = [[[EKEventStore alloc] init] autorelease];
if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {
// iOS 6 and later
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (granted) {
// code here for when the user allows your app to access the calendar
[self performCalendarActivity:eventStore];
} else {
// code here for when the user does NOT allow your app to access the calendar
}
}];
} else {
// code here for iOS < 6.0
[self performCalendarActivity:eventStore];
}