OStack程序员社区-中国程序员成长平台

标题: ios - EKEventStore-该事件不属于该事件存储 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 10:46
标题: ios - EKEventStore-该事件不属于该事件存储

我根本无法保存提醒/事件。但是,我已被授予使用提醒的权限(我检查了设置)。我已经打印出错误,并显示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)setAReminderid)sender {

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


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

    [newReminder addAlarmurAlarm];
    NSError *error = nil;

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

有什么建议可能是问题所在吗?



Best Answer-推荐答案


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

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

}

关于ios - EKEventStore-该事件不属于该事件存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24656252/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4