Failing to save EKReminder in iOS 6

偶尔善良 提交于 2019-12-06 06:20:14

问题


I am trying to save/ retrieve reminders from my app, but for some reason the EKReminder seems as though it is not being saved. Here is my code:

EKEventStore * _eventStore = [[EKEventStore alloc] init];
[_eventStore requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError *error) {

    //create a new calendar for reminders.
    EKCalendar *calendar = [EKCalendar calendarForEntityType:EKEntityTypeReminder eventStore:_eventStore];
    EKSource *localSource = nil;
    for (EKSource *source in _eventStore.sources)
        if (source.sourceType == EKSourceTypeLocal)
        {
            localSource = source;
            break;
        }

    calendar.source = localSource;

    self.calendarIdentifier = calendar.calendarIdentifier;
    EKReminder *reminder = [EKReminder reminderWithEventStore:_eventStore];
    reminder.calendar = calendar;
    reminder.title = @"Test Reminder";
    reminder.startDateComponents = [[NSCalendar currentCalendar] components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];
    reminder.completionDate = [NSDate dateWithTimeIntervalSinceNow:100];

    EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:[NSDate dateWithTimeIntervalSinceNow:100]];
    reminder.alarms = @[alarm];      

    NSError *errorOb = nil;
    BOOL saved = [_eventStore saveReminder:reminder commit:YES error:&errorOb];
    if (saved) NSLog(@"Saved Reminder");
    else NSLog(@"Failed to save reminder");

}];

Then later on, I am trying to verify that the reminder has in fact been saved. I do that by using this code:

NSPredicate *predicate = [_eventStore predicateForRemindersInCalendars:nil];
[_eventStore fetchRemindersMatchingPredicate:predicate completion:^(NSArray *reminders) {
    for (EKReminder *reminder in reminders) {
        NSLog(@"Reminder Title: %@", reminder.title);
    }
}];

And my new reminder never shows up!

Can anyone tell me what I am doing wrong here??


回答1:


You only need to call -requestAccessToEntityType:completion: if the authorization status is EKAuthorizationStatusNotDetermined. You can check the authorization status using +[EKEventStore authorizationStatusForEntityType:].

The completion block will then only be called the first time you call -requestAccessToEntityType:completion. On the Simulator it won't even be called once, because on the Simulator your app will already have access.

EKEventStore * _eventStore = [[EKEventStore alloc] init];
EKAuthorizationStatus authorizationStatus = [EKEventStore authorizationStatusForEntityType:EKEntityTypeReminder];
if (authorizationStatus == EKAuthorizationStatusNotDetermined) {
    [_eventStore requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError *error) {
        if (granted) {
            [self createNewReminder];
        } else {
            [self showNowAccessAlert];
        }
    }];
} else {
    if (authorizationStatus == EKAuthorizationStatusAuthorized) {
        [self createNewReminder];
    } else {
        [self showNowAccessAlert];
    }
}



回答2:


I got this to work by switching up the localSource to be EKSourceTypeCalDAV. Not totally sure why that was required, maybe someone can shed some light on that?




回答3:


Hope this works for you.

    if([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
    {
        // iOS 6 and later
        // asks user to allow application to use his device calendar
        [eventStore requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError *error)
        {
            if (granted)
            {
                EKReminder *reminder = [EKReminder reminderWithEventStore:eventStore];
                reminder.title = aNotification;
                reminder.calendar = [eventStore defaultCalendarForNewReminders];
                NSDate *date = //aDate;
                EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:date];
                [reminder addAlarm:alarm];
                NSError *error = nil;
                [eventStore saveReminder:reminder commit:YES error:&error];

                if(error)
                    NSLog(@"unable to Reminder!: Error= %@", error);
            }
        }];
    }
    // iOS < 6
    else
    {
        EKReminder *reminder = [EKReminder reminderWithEventStore:eventStore];
        reminder.title = aNotification;
        reminder.calendar = [eventStore defaultCalendarForNewReminders];
        NSDate *date = //aDate;
        EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:date];
        [reminder addAlarm:alarm];
        NSError *error = nil;
        [eventStore saveReminder:reminder commit:YES error:&error];

        if(error)
            NSLog(@"unable to Reminder!: Error= %@", error);
    }

And check my this post if you are having trouble in saving an event to device calendar.



来源:https://stackoverflow.com/questions/15237014/failing-to-save-ekreminder-in-ios-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!