EKEventStore - 该事件不属于该事件存储

时间:2014-07-09 14:20:26

标签: ios ekevent

我根本无法保存提醒/事件。我被授予使用提醒的权限(我检查了设置)。我打印出错误,并说That event does not belong to that event store.这是我的代码;

- (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]);
}

可能出现问题的任何建议?

1 个答案:

答案 0 :(得分:2)

我将代码更改为此代码并且工作正常。

 -(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);

}