简体   繁体   中英

EKReminder No calendar has been set

I don't understand why this code does not work anymore (it used to work). I create a UIDatePicker which triggers a reminder in the calendar at the specified date. I don't know why I get the error "No Calendar has been set" because I do set it (as can be seen in the code)

- (void)removeViews:(id)object {
    [[self.view viewWithTag:9] removeFromSuperview];
    [[self.view viewWithTag:10] removeFromSuperview];
    [[self.view viewWithTag:11] removeFromSuperview];
}

- (void)dismissDatePicker:(id)sender {
    if (eventStore == nil)
    {
        eventStore = [[EKEventStore alloc]init];
        EKAuthorizationStatus authorizationStatus = [EKEventStore authorizationStatusForEntityType:EKEntityTypeReminder];
        if (authorizationStatus == EKAuthorizationStatusNotDetermined) {
            [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
                if(granted){
                    [self createReminder];
                }

            }];
        }
        if (authorizationStatus == EKAuthorizationStatusAuthorized) {
            [self createReminder];
        }

    }

    if (eventStore != nil){

        [self createReminder];
    }
    CGRect toolbarTargetFrame = CGRectMake(self.view.bounds.size.width/2-160, self.view.bounds.size.height/2-44-108, 320, 44);
    CGRect datePickerTargetFrame = CGRectMake(self.view.bounds.size.width/2-160, self.view.bounds.size.height/2-108, 320, 216);
    [UIView beginAnimations:@"MoveOut" context:nil];
    [self.view viewWithTag:9].alpha = 0;
    [self.view viewWithTag:10].frame = datePickerTargetFrame;
    [self.view viewWithTag:11].frame = toolbarTargetFrame;
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(removeViews:)];
    [UIView commitAnimations];
    NSLog(@"dismiss");
}

- (IBAction)callDP:(id)sender {
    if ([self.view viewWithTag:9]) {
        return;
    }
    CGRect toolbarTargetFrame = CGRectMake(self.view.bounds.size.width/2-160, self.view.bounds.size.height/2-44-108, 320, 44);
    CGRect datePickerTargetFrame = CGRectMake(self.view.bounds.size.width/2-160, self.view.bounds.size.height/2-108, 320, 216);

    datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(self.view.bounds.size.width/2-160, self.view.bounds.size.height+44, 320, 216)];
    datePicker.tag = 10;
    [datePicker setBackgroundColor:[UIColor whiteColor]];
    datePicker.alpha = 0.87;
    [self.view addSubview:datePicker];

    UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height+44, 320, 44)];
    toolBar.tag = 11;
    toolBar.barStyle = UIBarStyleDefault ;
    UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissDatePicker:)];
    [toolBar setItems:[NSArray arrayWithObjects:spacer, doneButton, nil]];
    [self.view addSubview:toolBar];

    [UIView beginAnimations:@"MoveIn" context:nil];
    toolBar.frame = toolbarTargetFrame;
    datePicker.frame = datePickerTargetFrame;
    [UIView commitAnimations];
}
-(void)createReminder
{
    EKReminder *reminder = [EKReminder
                            reminderWithEventStore:eventStore];

    reminder.title = @"it's time to do your ELS exercise";

    reminder.calendar = [eventStore defaultCalendarForNewReminders];

    NSDate *date = [datePicker date];

    EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:date];

    [reminder addAlarm:alarm];

    NSError *error = nil;

    [eventStore saveReminder:reminder commit:YES error:&error];

    if (error){
        NSLog(@"error = %@", error);
    }
}

I get the error :

Domain=EKErrorDomain Code=1 "No calendar has been set." UserInfo=0x17826b400 {NSLocalizedDescription=No calendar has been set.}

Solution : The event store was never instantiated because it was not nil. I added in viewDidLoad:

eventStore = [[EKEventStore alloc]init];

and I kept only the following in dismissDatePicker :

[eventStore requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError *error) {
        [self createReminder];
    }];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM