我有一段代码试图使用OS X 10.8及更新版本中的EventKit
框架来保存通用提醒。这是我的代码片段:
NSLog(@"Creating reminder.");
EKEventStore *eventStore = [[EKEventStore alloc] initWithAccessToEntityTypes:EKEntityMaskReminder];
NSLog(@"Auth status: %lu", [EKEventStore authorizationStatusForEntityType:EKEntityTypeReminder]);
[eventStore requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError *error) {
NSLog(@"Granted: %d Error: %@", granted, error);
if (granted) {
EKReminder *reminder = [EKReminder reminderWithEventStore:eventStore];
reminder.title = @"My Reminder";
EKCalendar *reminderCalendar = [EKCalendar calendarForEntityType:EKEntityTypeReminder eventStore:eventStore];
NSLog(@"Using EKCalendar: %@", reminderCalendar);
reminder.calendar = reminderCalendar;
NSError *commitError = nil;
if (![eventStore saveReminder:reminder commit:YES error:&commitError] || commitError) {
NSLog(@"An error occurred committing the reminder: %@", commitError);
} else {
NSLog(@"Reminder created successfully.");
}
}
}];
这是我看到的输出:
2014-01-08 19:56:21.182 MyApp[6073:303] Creating reminder.
2014-01-08 19:56:21.358 MyApp[6073:303] Auth status: 3
2014-01-08 19:56:21.358 MyApp[6073:3423] Granted: 1 Error: (null)
2014-01-08 19:56:21.359 MyApp[6073:3423] Using EKCalendar: EKCalendar <0x9c430>, calendarIdentifier:9BD66DA9-014D-4918-849A-1D6763994511, allowEvents:0, allowReminders:1, allowsContentModifications:1, isSubscribed:0, isDeletable:1, isRenameable:1, isShareable:0, defaultOrganizerIsMeForNewItems:0
2014-01-08 19:56:21.366 MyApp[6073:3423] An error occurred committing the reminder: Error Domain=EKErrorDomain Code=1 "No calendar has been set." UserInfo=0x60000067d900 {NSLocalizedDescription=No calendar has been set.}
但是,尽管出现错误消息,EKCalendar
已成功创建/访问,和已在EKReminder
对象上设置。
我在iOS上使用EventKit
时发现此错误的其他类似资源,其获取访问权限的方法略有不同。
您可以使用reminderWithEventStore:类创建提醒 方法。标题和日历属性是必需的。日历 提醒是用于分组的列表。
但我一定做错了。在我的App Sandbox/App Data
权限下,我选择Calendar
和Contacts
只是为了安全,但错误似乎与权限无关。
答案 0 :(得分:3)
而不是使用:
EKCalendar *reminderCalendar = [EKCalendar calendarForEntityType:EKEntityTypeReminder eventStore:eventStore];
NSLog(@"Using EKCalendar: %@", reminderCalendar);
reminder.calendar = reminderCalendar;
我能够使用:
成功创建EKReminder
reminder.calendar = [eventStore defaultCalendarForNewReminders];