获取新事件的默认日历时出错:错误域= EKCADErrorDomain代码= 1013"(null)"

时间:2017-05-02 07:43:50

标签: objective-c events calendar

我正在尝试从我的应用程序中保存iPhone日历上的事件,我使用此代码:

- (void)addReminderWithTitle:(NSString *)title date:(NSString *)date {
    NSString *dateString = date;
    NSDateFormatter *dateFormatter  = [NSDateFormatter new];
    [dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
    NSDate *dateFromString = [dateFormatter dateFromString:dateString];
    [dateFromString dateByAddingTimeInterval:-60*30];

      EKEventStore *eventStore = [[EKEventStore alloc] init];
    [eventStore requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError * _Nullable error) {
        if (granted) {
            EKEvent *event = [EKEvent eventWithEventStore:eventStore];
            event.title = @"Event Title";
            event.startDate = [NSDate date]; //today
            event.endDate = [event.startDate dateByAddingTimeInterval:60*60];  //set 1 hour meeting
            event.calendar = [eventStore defaultCalendarForNewEvents];
            NSError *err = nil;
            [eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&err];

它没有保存任何错误,我得到了这个: 获取新事件的默认日历时出错:错误域= EKCADErrorDomain代码= 1013"(null)"

任何帮助?

2 个答案:

答案 0 :(得分:1)

我使用您的代码我在info.plist NSCalendarsUsageDescription NSRemindersUsageDescription中添加了两个属性 然后我NSLog(@“eventStore =:%@”,eventStore);结果是eventStore=:<EKEventStore: 0x17418ce60>

我的代码:

- (void)viewDidLoad {
 [super viewDidLoad];
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];

NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
[dateFromString dateByAddingTimeInterval:-60*30];

EKEventStore *eventStore = [[EKEventStore alloc] init];
[eventStore requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError * _Nullable error) {
    if (granted) {
        EKEvent *event = [EKEvent eventWithEventStore:eventStore];
        event.title = @"Event Title";
        event.startDate = [NSDate date]; //today
        event.endDate = [event.startDate dateByAddingTimeInterval:60*60];  //set 1 hour meeting
        event.calendar = [eventStore defaultCalendarForNewEvents];
        NSError *err = nil;
        [eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&err];

    }}];

  NSLog(@"eventStore=:%@", eventStore);
}

答案 1 :(得分:0)

这是您正在尝试做的快速代码。您正在请求访问以处理提醒而不是事件,这就是您收到此错误的原因。

let store = EKEventStore()

// Request access to calendar first
store.requestAccess(to: .event, completion: { (granted, error) in
    if granted {
            print("calendar allowed")

            // create the event object
            let event = EKEvent(eventStore: store)
            event.title = self.activityPara.actName
            event.startDate = self.activityPara.actDate
            event.endDate = self.activityPara.actDate.addingTimeInterval(3600) // 1 hr time
            event.location = self.activityPara.actLocatnStr
            event.calendar = store.defaultCalendarForNewEvents

            let controller = EKEventEditViewController()
            controller.event = event
            controller.eventStore = store
            controller.editViewDelegate = self
            self.present(controller, animated: true)
        }
        else
        {
            print("calendar not allowed")
        }           
    })

另外,请确保在info.plist中包含隐私 - 日历使用说明。