有人可以帮我弄清楚我在这里做错了什么。我的应用程序应该访问 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 将得到一个对象。这让我发疯——为什么它只在我退出应用程序后才起作用(而其他日历家庭、工作和生日)似乎都立即出现?
有什么想法吗?
仅供参考,我查看了这个相关问题,它帮助我找出了第一批日历事件的最初问题,但这对提醒没有帮助