3

有人可以帮我弄清楚我在这里做错了什么。我的应用程序应该访问 iPhone 的各种日历以检查即将发生的事件。所以我需要访问日历中的“事件”以及“提醒”。当我有事件时,我将它们临时存储在 UserDefaults

在我的 .h 文件中,我有这样的东西

@property (nonatomic, strong) EKEventStore *eventStore;
@property (nonatomic, strong) NSMutableArray *calendars;
@property (nonatomic, strong) NSMutableArray *reminders;
@property (nonatomic) BOOL accessToCalendarsGranted;
@property (nonatomic) BOOL accessToRemindersGranted;

我需要获得用户的许可才能访问这些,所以在主代码中我有这样的代码:

    -(void)requestCalendarAccess
{
    // Prompt the user for access to their Calendar

    [_eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error){
        if (!granted)
        {
            NSLog(@"user has not granted access to calendars!!");
            // Display a message if the user has denied or restricted access to Calendar
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Privacy Warning", nil)
                                                            message:NSLocalizedString(@"Permission was not granted for Calendar", nil)
                                                           delegate:nil
                                                  cancelButtonTitle:NSLocalizedString(@"Dismiss", nil)
                                                  otherButtonTitles:nil];
            [alert show];

            return;
        }

        // Let's ensure that our code will be executed from the main queue
        dispatch_async(dispatch_get_main_queue(), ^{
            // The user has granted access to their Calendar
            [self accessGrantedForCalendar];
        });
    }];
}

由 accessGrantedForCalendar 中的代码完成

_calendars = [[NSUserDefaults standardUserDefaults] objectForKey:kCalendarListKey];

//if we have calendars stored in user defaults, we go and read them
if(!_calendars)
{
    _calendars = [[NSMutableArray alloc] init];
    NSArray *phoneCalendarArray = [_eventStore calendarsForEntityType:EKEntityTypeEvent];

for (EKCalendar *systemCalendar in phoneCalendarArray)
{
    NSMutableDictionary *calendarDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:[systemCalendar title], kCalendarName, [systemCalendar calendarIdentifier], kCalendarIndentifier, [NSNumber numberWithBool:YES], kCalendarWatched,  nil];

    [_calendars addObject:calendarDict];
}

[[NSUserDefaults standardUserDefaults] setObject:_calendars forKey:kCalendarListKey];
[[NSUserDefaults standardUserDefaults] synchronize];

}

现在这一切都运作良好。许多其他用于实际获取事件的代码未包含在此处。当我尝试使用相同的提醒方法时,问题就来了:

-(void)requestRemindersAccess
{
    [self.eventStore requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError *error){
        if (granted)
        {
            // Let's ensure that our code will be executed from the main queue
            dispatch_async(dispatch_get_main_queue(), ^{
            // The user has granted access to their Calendar
                [self accessGrantedForReminder];
            });
        }
        else
        {
            NSLog(@"user has not granted access to reminders!!");
            // Display a message if the user has denied or restricted access to Calendar
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Privacy Warning", nil)
                                                            message:NSLocalizedString(@"Permission was not granted for Reminders", nil)
                                                           delegate:nil
                                                  cancelButtonTitle:NSLocalizedString(@"Dismiss", nil)
                                                  otherButtonTitles:nil];
            [alert show];
        }
    }];
}

以及在 accessGrantedForReminder 中完成此操作的代码:

_reminders = [[NSUserDefaults standardUserDefaults] objectForKey:kRemindersListKey];

//if we don't have reminders stored in user defaults, we go and read them
if(!_reminders || _reminders.count == 0)
{
    _reminders = [[NSMutableArray alloc] init];
    //FOLLOWING LINE HAS PROBLEM FIRST TIME!!!
    NSArray *phoneReminderArray = [_eventStore calendarsForEntityType:EKEntityTypeReminder];

for (EKCalendar *systemCalendar in phoneReminderArray)
{
    NSMutableDictionary *calendarDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:[systemCalendar title], kCalendarName, [systemCalendar calendarIdentifier], kCalendarIndentifier, [NSNumber numberWithBool:YES], kCalendarWatched,  nil];

    [_reminders addObject:calendarDict];
}

[[NSUserDefaults standardUserDefaults] setObject:_reminders forKey:kRemindersListKey];
[[NSUserDefaults standardUserDefaults] synchronize];

}

上面指出的行有问题——我第一次运行应用程序时(即在获得许可后立即)我看到 phoneReminderArray 中有零个对象。如果我退出程序并再次运行 phoneReminderArray 将得到一个对象。这让我发疯——为什么它只在我退出应用程序后才起作用(而其他日历家庭、工作和生日)似乎都立即出现?

有什么想法吗?

仅供参考,我查看了这个相关问题,它帮助我找出了第一批日历事件的最初问题,但这对提醒没有帮助

4

2 回答 2

3

解决方案其实很简单。在您调用以下行以获取提醒之前:

  NSArray *phoneReminderArray = [_eventStore calendarsForEntityType:EKEntityTypeReminder];

放置这一行:

[_eventStore reset];

这应该可以解决您的问题。

于 2013-11-13T13:02:30.020 回答
0

在塔拉沙尔的回答之后我无法让它工作,我也尝试了类似的refreshSourcesIfNecessary方法,但无济于事。

什么对我有用:在询问提醒日历列表之前,我必须拨打此电话:

[_eventStore defaultCalendarForNewReminders];

之后,我可以通过以下方式获取提醒日历列表:

[_eventStore calendarsForEntityType:EKEntityTypeReminder]
于 2015-12-18T21:21:20.513 回答