我試圖從我的應用保存/檢索提醒,但由於某種原因,EKReminder好像並未保存。這裏是我的代碼:無法在iOS 6中保存EKReminder
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");
}];
然後後來,我試圖驗證該提醒實際上已被保存。我通過使用此代碼來實現這一點:
NSPredicate *predicate = [_eventStore predicateForRemindersInCalendars:nil];
[_eventStore fetchRemindersMatchingPredicate:predicate completion:^(NSArray *reminders) {
for (EKReminder *reminder in reminders) {
NSLog(@"Reminder Title: %@", reminder.title);
}
}];
而我的新提醒永遠不會顯示!
任何人都可以告訴我我在做什麼錯嗎?
這是真的。感謝您的洞察力 – 2013-03-07 00:01:30