defaultCalendarForNewEvents failed

When I try to call [newEventStore defaultCalendarForNewEvents], it returns an error message says:

[707:907] defaultCalendarForNewEvents failed: Error Domain=EKCADErrorDomain Code=1013 "The operation couldn’t be completed. (EKCADErrorDomain error 1013.)"
[707:907] Error!Failed to save appointment. Description:Error Domain=EKErrorDomain Code=1 "No calendar has been set." UserInfo=0x1fc679f0 {NSLocalizedDescription=No calendar has been set.}

The app runs for a long time. The problem came to me first time. The phone runs IOS6 Beta4. model is iphone 4. Is there anyone knows when the defaultCalendarForNewEvents method will get failed? I can not get any useful information from googling.


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

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

iOS6+ requires users authentication to save event to his device calendar. Here is a code snippt:

    // save to iphone calendar
    EKEventStore *eventStore = [[EKEventStore alloc] init];
    if([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
    {
        // iOS 6 and later
        // This line asks user's permission to access his calendar
        [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
        {
            if (granted) // user user is ok with it
            {
                EKEvent *event = [EKEvent eventWithEventStore:eventStore];
                event.title  = aTitle;
                event.allDay = YES;

                NSDateFormatter *dateFormat = [[UIApplicationSingleton sharedManager] aDateFormatter];
                [dateFormat setDateFormat:@"MMM dd, yyyy hh:mm aaa"];
                event.startDate = event.endDate = //put here if start and end dates are same

                [event setCalendar:[eventStore defaultCalendarForNewEvents]];
                NSError *err;

                [eventStore saveEvent:event span:EKSpanThisEvent error:&err];

                if(err)
                    NSLog(@"unable to save event to the calendar!: Error= %@", err);

            }
            else // if he does not allow 
            {
                [[[UIAlertView alloc]initWithTitle:nil message:alertTitle delegate:nil cancelButtonTitle:NSLocalizedString(@"plzAlowCalendar", nil)  otherButtonTitles: nil] show];
                return;
            }
        }];
    }

    // iOS < 6
    else
    {
        EKEvent *event = [EKEvent eventWithEventStore:eventStore];
        event.title  = aTitle;
        event.allDay = YES;

        NSDateFormatter *dateFormat = [[UIApplicationSingleton sharedManager] aDateFormatter];
        [dateFormat setDateFormat:@"MMM dd, yyyy hh:mm aaa"];
        event.startDate = event.endDate = //put here if start and end dates are same

        [event setCalendar:[eventStore defaultCalendarForNewEvents]];
        NSError *err;

        [eventStore saveEvent:event span:EKSpanThisEvent error:&err];

        if(err)
            NSLog(@"unable to save event to the calendar!: Error= %@", err);

    }

And check my this post if you are facing trouble in setting alarm to the application.