Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

defaultCalendarForNewEvents failed

Tags:

ios

calendar

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.

like image 372
Alex.BIG Avatar asked Sep 17 '12 06:09

Alex.BIG


6 Answers

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];
}
like image 193
yunas Avatar answered Oct 29 '22 10:10

yunas


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;
  }
like image 37
Alphonse R. Dsouza Avatar answered Oct 29 '22 09:10

Alphonse R. Dsouza


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.

like image 40
Vaibhav Saran Avatar answered Oct 29 '22 10:10

Vaibhav Saran


Solved. I accidentally turn off the app access to calendar in Setting->Privacy on IOS6

like image 32
Alex.BIG Avatar answered Oct 29 '22 11:10

Alex.BIG


I had the same problem, but finally find what was the reason.

My case was to add my Reminder and Calendar events, but I was using one EKEventStore. In the end I seperate them and problem dissapeared:

private static let calendarEventStore = EKEventStore()
private static let remindersEventStore = EKEventStore()

So now I'm using calendarEventStore for all things related to calendar event and remindersEventStore for reminder one.

——

In my opinion it was related to the fact that I requested defaultCalendarForNewEvents and defaultCalendarForNewReminders() in one EKEventStore entity.

Also this one from EKEventStore docs:

Events, Reminders, and Calendar objects retrieved from an event store cannot be used with any other event store

like image 41
Alexander Zimin Avatar answered Oct 29 '22 11:10

Alexander Zimin


Swift form

(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

like image 20
Maxim Shoustin Avatar answered Oct 29 '22 10:10

Maxim Shoustin