Defaultcalendarfornewevents Failed

defaultCalendarForNewEvents is defined as optional however can't use optional binding to check if it's nil

I asked this question at the Swift discussion forum at swift.org and got a response. So as per the response, 'defaultCalendarForNewEvents' was marked non-optional in Swift 3 by accident and that was fixed in Swift 4. That's why there was a discrepancy: documentation showing declaration in Swift 4 but optional binding failing as I'm on Swift 3. Hope this helps someone who is having the same issue.

I was also told that this issue was not release-noted as it was a minor update.

Error getting default calendar for new events: Error Domain=EKCADErrorDomain Code=1013 (null)

i use your code i add added two properties in info.plist NSCalendarsUsageDescription NSRemindersUsageDescription

then I NSLog(@"eventStore=:%@", eventStore); the result is eventStore=:<EKEventStore: 0x17418ce60>

my code:

- (void)viewDidLoad {
[super viewDidLoad];
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];

NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
[dateFromString dateByAddingTimeInterval:-60*30];

EKEventStore *eventStore = [[EKEventStore alloc] init];
[eventStore requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = @"Event Title";
event.startDate = [NSDate date]; //today
event.endDate = [event.startDate dateByAddingTimeInterval:60*60]; //set 1 hour meeting
event.calendar = [eventStore defaultCalendarForNewEvents];
NSError *err = nil;
[eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&err];

}}];

NSLog(@"eventStore=:%@", eventStore);
}

iOS: EKEventStore sources / defaultCalendarForNewEvents / calendarsForEntityType all return nothing AFTER authorization

OK, it turns out there was a small line of code in a viewDidLoad that was before the [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error code and that tried to access the eventStore. It was superfluous and did not need to be happening and I had not noticed it. Once I removed this code the stuff now works, so it does go back to needing to authorize, as even after authorization, the eventStore was already "bad" due to this previous access.



Related Topics



Leave a reply



Submit