有人可以帮我弄清楚我在做什么错。我的应用程序应该访问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将得到一个对象。这让我发疯了-为什么它仅在我退出应用程序后才起作用(而其他日历“家庭,工作和生日”日历都似乎马上出现了?
有任何想法吗?
FYI,我查看了此related question,它帮助我找出了第一批日历事件的最初问题,但对提醒没有帮助
有人可以帮我弄清楚我在做什么错。我的应用程序应该访问iPhone的各种日历以检查即将发生的事件。因此,我需要访问日历中的“事件”,还需要...
解决方案实际上非常简单。在您致电以下行以获取提醒之前:
在talashar回答后,我无法使它工作,我也尝试了类似的refreshSourcesIfNecessary
方法,但无济于事。
Swift版本