有人可以帮我弄清楚我在这里做错了什么。我的应用程序应该访问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将获得一个对象。它让我疯了 - 为什么它只在我退出应用程序后才能工作(而其他日历,Home,Work& Birthdays)似乎都会马上出现?
任何想法?
仅供参考我查看了这个related question,它帮助我找出了我的第一批日历事件的初始问题,但它没有帮助提醒
答案 0 :(得分:3)
解决方案实际上非常简单。 在您拨打以下行以获取提醒之前:
NSArray *phoneReminderArray = [_eventStore calendarsForEntityType:EKEntityTypeReminder];
放置这一行:
[_eventStore reset];
这可以解决你的问题。
答案 1 :(得分:0)
在talashar的回答之后,我无法让它工作,我也试过类似的refreshSourcesIfNecessary
方法无济于事。
对我有用的是:在询问提醒日历列表之前,我必须先拨打此行:
[_eventStore defaultCalendarForNewReminders];
之后,我能够获得提醒日历列表:
[_eventStore calendarsForEntityType:EKEntityTypeReminder]