gpt4 book ai didi

ios - calendarsForEntityType :EKEntityTypeReminder is empty first time, 随后工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:07:51 24 4
gpt4 key购买 nike

谁能帮我弄清楚我在这里做错了什么。我的应用程序应该访问 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 将获得一个对象。这让我抓狂 - 为什么只有在我退出应用程序后它才有效(而其他日历家庭、工作和生日)似乎都立即出现?

有什么想法吗?

仅供引用,我看了这个related question它帮助我找出了第一批日历事件的最初问题,但它对提醒没有帮助

最佳答案

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

  NSArray *phoneReminderArray = [_eventStore calendarsForEntityType:EKEntityTypeReminder];

放置这一行:

[_eventStore reset];

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

关于ios - calendarsForEntityType :EKEntityTypeReminder is empty first time, 随后工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19907063/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com