简体   繁体   English

无法在iOS 6中保存EKReminder

[英]Failing to save EKReminder in iOS 6

I am trying to save/ retrieve reminders from my app, but for some reason the EKReminder seems as though it is not being saved. 我试图从我的应用程序中保存/检索提醒,但是由于某种原因,EKReminder似乎好像没有保存。 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?? 谁能告诉我我在做什么错?

You only need to call -requestAccessToEntityType:completion: if the authorization status is EKAuthorizationStatusNotDetermined . 如果授权状态为EKAuthorizationStatusNotDetermined则仅需要调用-requestAccessToEntityType:completion: EKAuthorizationStatusNotDetermined You can check the authorization status using +[EKEventStore authorizationStatusForEntityType:] . 您可以使用+[EKEventStore authorizationStatusForEntityType:]检查授权状态。

The completion block will then only be called the first time you call -requestAccessToEntityType:completion . 然后,仅在您首次调用-requestAccessToEntityType:completion时才调用完成块。 On the Simulator it won't even be called once, because on the Simulator your app will already have access. 在Simulator上甚至不会调用一次,因为在Simulator上您的应用程序已经可以访问。

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

I got this to work by switching up the localSource to be EKSourceTypeCalDAV. 我通过将localSource切换为EKSourceTypeCalDAV来使其工作。 Not totally sure why that was required, maybe someone can shed some light on that? 不能完全确定为什么要这样做,也许有人可以对此有所了解?

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. 如果您无法将事件保存到设备日历中,请查看我的这篇文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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