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
Didreceiveremotenotification Not Working in the Background
Date to Milliseconds and Back to Date in Swift
Table View Images Never Being Released
Xcode Canvas for Swiftui Previews Does Not Show Up
How to Manually Deprecate Members
Using Audiobufferlist with Swift
Passing Data to View Controllers That Are Embedded in Container Views
iOS Nsdate() Returns Incorrect Time
Filemanager Cannot Find Audio File
Error: Initializer for Conditional Binding Must Have Optional Type, Not 'String'
How to Change Wkwebview or Uiwebview Default Font
Using Nsuserdefaults with Xcode 8 and iOS 10
Monotouch/Wcf: How to Consume the Wcf Service Without Svcutil
Swift Countelements() Return Incorrect Value When Count Flag Emoji
Code Sign Error:Bundle Format Unrecognized, Invalid, or Unsuitable
How to Dynamically Change the Font Size in Auto Layout iOS
Xcode 7 Swift 2 Impossible to Instantiate Uiviewcontroller Subclass of Generic Uitableviewcontroller