无法在iOS 6中保存EKReminder [英] Failing to save EKReminder in iOS 6
问题描述
我正在尝试从我的应用程序中保存/检索提醒,但是由于某种原因,EKReminder似乎没有被保存.这是我的代码:
I am trying to save/ retrieve reminders from my app, but for some reason the EKReminder seems as though it is not being saved. Here is my code:
EKEventStore * _eventStore = [[EKEventStore alloc] init];
[_eventStore requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError *error) {
//create a new calendar for reminders.
EKCalendar *calendar = [EKCalendar calendarForEntityType:EKEntityTypeReminder eventStore:_eventStore];
EKSource *localSource = nil;
for (EKSource *source in _eventStore.sources)
if (source.sourceType == EKSourceTypeLocal)
{
localSource = source;
break;
}
calendar.source = localSource;
self.calendarIdentifier = calendar.calendarIdentifier;
EKReminder *reminder = [EKReminder reminderWithEventStore:_eventStore];
reminder.calendar = calendar;
reminder.title = @"Test Reminder";
reminder.startDateComponents = [[NSCalendar currentCalendar] components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];
reminder.completionDate = [NSDate dateWithTimeIntervalSinceNow:100];
EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:[NSDate dateWithTimeIntervalSinceNow:100]];
reminder.alarms = @[alarm];
NSError *errorOb = nil;
BOOL saved = [_eventStore saveReminder:reminder commit:YES error:&errorOb];
if (saved) NSLog(@"Saved Reminder");
else NSLog(@"Failed to save reminder");
}];
然后,我试图验证该提醒是否确实已保存.我通过使用以下代码来做到这一点:
Then later on, I am trying to verify that the reminder has in fact been saved. I do that by using this code:
NSPredicate *predicate = [_eventStore predicateForRemindersInCalendars:nil];
[_eventStore fetchRemindersMatchingPredicate:predicate completion:^(NSArray *reminders) {
for (EKReminder *reminder in reminders) {
NSLog(@"Reminder Title: %@", reminder.title);
}
}];
我的新提醒永远不会出现!
And my new reminder never shows up!
有人可以告诉我我在做什么错吗?
Can anyone tell me what I am doing wrong here??
推荐答案
如果授权状态为 EKAuthorizationStatusNotDetermined
,则只需调用 -requestAccessToEntityType:completion:
.您可以使用 + [EKEventStoreauthorizationStatusForEntityType:]
检查授权状态.
You only need to call -requestAccessToEntityType:completion:
if the authorization status is EKAuthorizationStatusNotDetermined
. You can check the authorization status using +[EKEventStore authorizationStatusForEntityType:]
.
仅当您第一次调用 -requestAccessToEntityType:completion
时,才会调用完成块.在Simulator上甚至都不会被调用一次,因为在Simulator上,您的应用已经可以访问.
The completion block will then only be called the first time you call -requestAccessToEntityType:completion
. On the Simulator it won't even be called once, because on the Simulator your app will already have access.
EKEventStore * _eventStore = [[EKEventStore alloc] init];
EKAuthorizationStatus authorizationStatus = [EKEventStore authorizationStatusForEntityType:EKEntityTypeReminder];
if (authorizationStatus == EKAuthorizationStatusNotDetermined) {
[_eventStore requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError *error) {
if (granted) {
[self createNewReminder];
} else {
[self showNowAccessAlert];
}
}];
} else {
if (authorizationStatus == EKAuthorizationStatusAuthorized) {
[self createNewReminder];
} else {
[self showNowAccessAlert];
}
}
这篇关于无法在iOS 6中保存EKReminder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!