DevC
DevC

Reputation: 7022

EKEventStore -That event does not belong to that event store

I am not able to save my reminders/events at all. I have been granted permission to use the reminders ( I checked the settings) however. I have printed out the error and it says That event does not belong to that event store. Here is my code;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.eventStore =[[EKEventStore alloc] init];
    self.eventStoreAccessGranted = NO;
    [self.eventStore requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError *error) {
        self.eventStoreAccessGranted = granted;
        if (!granted) {
            NSLog(@"User has not granted permission");
        }
    }];

 }



- (IBAction)setAReminder:(id)sender {

    if (!self.eventStoreAccessGranted) {
        NSLog(@"Reminder is no");
        return;
    }
    NSLog(@"Reminder is YES");
    EKReminder * newReminder = [[EKReminder alloc] init];
    newReminder.title = @"Pick up the kids";
    newReminder.calendar = [self.eventStore defaultCalendarForNewReminders];


    NSDate *now = [NSDate date];
    NSDate *alarmDate = [now dateByAddingTimeInterval:120];
    EKAlarm * ourAlarm = [EKAlarm alarmWithAbsoluteDate:alarmDate];

    [newReminder addAlarm:ourAlarm];
    NSError *error = nil;

    [self.eventStore saveReminder:newReminder commit:YES error:&error];
    NSLog(@"Event %@",[error localizedDescription]);
}

Any suggestions where the problem may be?

Upvotes: 2

Views: 1825

Answers (1)

DevC
DevC

Reputation: 7022

I changed my code to this and it works correctly.

 -(IBAction)setAReminder:(id)sender {

    if (self.eventStore == nil)
    {
        self.eventStore = [[EKEventStore alloc]init];

        [self.eventStore requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError *error) {

        if (!granted)
            NSLog(@"Access to store not granted");
        }];
    }

    if (self.eventStore != nil)
        [self createReminder];

}

-(void)createReminder
{
    EKReminder *reminder = [EKReminder
                        reminderWithEventStore:self.eventStore];

    reminder.title = @"Test";

    reminder.calendar = [_eventStore defaultCalendarForNewReminders];

    NSDate *date = [NSDate date];

    EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:[date dateByAddingTimeInterval:120]];

    [reminder addAlarm:alarm];

     NSError *error = nil;

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

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

}

Upvotes: 1

Related Questions