3

I execute the following code to let the user choose multiple calendars to use for my notepad app. Until iOS 10.3.1, there was no problem. On 11.0.2, it was still working on actural devices. However, since 11.1 it crashes with the following error.

 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSDictionaryM setObject:forKeyedSubscript:]: key cannot be nil'

The code is as follows. Basically, I'm opening a blank calendar chooser.

if (_eventStore == nil) {
    _eventStore = [[EKEventStore alloc] init];
}
// the selector is available, so we must be on iOS 6 or newer
[_eventStore requestAccessToEntityType:EKEntityTypeEvent
                           completion:^(BOOL granted, NSError *error) {
                               dispatch_async(dispatch_get_main_queue(), ^{
                                   if (error)
                                   {
                                       // display error message here
                                   }
                                   else if (!granted)
                                   {
                                       // display access denied error message here
                                   }
                                   else
                                   {
                                       // access granted

                                       EKCalendarChooser *calendarChooser = [[EKCalendarChooser alloc]
                                                                             initWithSelectionStyle:EKCalendarChooserSelectionStyleMultiple
                                                                             displayStyle:EKCalendarChooserDisplayAllCalendars
                                                                             eventStore:_eventStore];

                                       calendarChooser.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
                                       calendarChooser.delegate = self;
                                       calendarChooser.showsCancelButton = YES;
                                       calendarChooser.showsDoneButton = YES;


                                           NSSet *calendarSet = [[NSSet alloc] init]; 
                                           calendarChooser.selectedCalendars = calendarSet;


                                       UINavigationController *sub = [[UINavigationController alloc] initWithRootViewController:calendarChooser];
                                       sub.navigationBar.barStyle = UIBarStyleDefault;
                                       sub.toolbar.barStyle = UIBarStyleDefault;
                                       [self presentViewController:sub animated:YES completion:nil];
                                       //ios11 crashes after this
                                   }
                               });
                           }];

Thanks for your help.

tsuyoski
  • 614
  • 5
  • 22

1 Answers1

4

It turns out that EKCalendarChooserDisplayAllCalendars was causing the crash. Although it's not ideal, now I can avoid the crash when iOS is 11.1 or higher.

                                       EKCalendarChooserDisplayStyle displayStyle = EKCalendarChooserDisplayAllCalendars;
                                       if (@available(iOS 11.1, *)) {
                                           displayStyle = EKCalendarChooserDisplayWritableCalendarsOnly;
                                       }
                                       EKCalendarChooser *calendarChooser = [[EKCalendarChooser alloc]
                                                                             initWithSelectionStyle:EKCalendarChooserSelectionStyleMultiple
                                                                             displayStyle:displayStyle
                                                                             eventStore:eventStore]; 
tsuyoski
  • 614
  • 5
  • 22
  • Ran into the same problem. This is really bad if I can access only writable calendars. – Victor Engel Nov 03 '17 at 23:36
  • Yes, absolutely terrible. At least Apple is acknowleged that there are issues. (Initializing an EKCalendarChooser from EventKit can result in an app crash. (34608102)) https://developer.apple.com/library/content/releasenotes/General/RN-iOS-11.1/index.html#//apple_ref/doc/uid/TP40017683-CH1-SW1 – tsuyoski Nov 05 '17 at 17:30
  • I found another bug this weekend. If you save an EKEvent with a start and end time between 1AM and 2AM during the autumn time change, if the time is the second occurrence of 1AM to 2AM, the database subtracts an hour to the end time (but not the start time), resulting in an event that starts after it ends. I filed a bug report, but I'm curious if anyone else can duplicate this bug. Note that you will need to query the raw date since both dates will format the same using the locale date format. – Victor Engel Nov 06 '17 at 00:42
  • I am curious of your discovery. Can you post it as a new question and let me know its URL? Thanks. – tsuyoski Nov 06 '17 at 09:17
  • 1
    https://stackoverflow.com/questions/47139246/daylight-saving-time-bug-in-eventkit – Victor Engel Nov 06 '17 at 14:31
  • @yoshitech, I'm a bit confused about why the `displayStyle` value was causing a problem and how you figured out that the fix was to use `EKCalendarChooserDisplayWritableCalendarsOnly`. I'm encountering an issue where occasionally users on iOS 11 are seeing an empty calendar chooser (which I am initializing with `EKCalendarChooserDisplayAllCalendars`), but my app doesn't crash, so I don't whether the displayStyle value might be causing the problem. Do you have any more information about the issue you experienced or how you identified this fix? – Natalia Sep 14 '18 at 23:03
  • @Natalia - I'm assuming most people had to start looking at the underlying data in `EKStore`, to make sure the ~chooser is seeing the right data... but once you start doing that, you're just a `UITableView` way from making your own picker... (but it does take a fair amount of polish to make it work correctly). – benc Jul 14 '23 at 19:42