获取实体类型 3、Xcode 6.1.1 的共享日历邀请时出错。EKCalender、EKSource、EKEventstore 和 Objective C

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28258204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:34:58  来源:igfitidea点击:

Error getting shared calendar invitations for entity types 3, Xcode 6.1.1. EKCalender, EKSource, EKEventstore and Objective C

objective-ciphonexcodeekeventstoreekeventkit

提问by user2511630

I am developing calendar app. I am trying to save EKEvent using assigned EKCalender. But when I try to run following code it gives me error. Please help

我正在开发日历应用程序。我正在尝试使用分配的 EKCalender 保存 EKEvent。但是当我尝试运行以下代码时,它给了我错误。请帮忙

-(BOOL)createEventWithTitle:(NSString *)paramTitle startDate:(NSDate *)paramStartDate endDate:(NSDate *)paramEndDate inCalendar:(EKCalendar *)paramCalendar inEventStore:(EKEventStore *)paramStore notes:(NSString *)paramNotes 
        {
         BOOL result = NO;

        //paramCalendar = [self.eventStoreiReportShifts defaultCalendarForNewEvents];
            if (self.eventsAccessGranted) {
                NSArray *caledars = [self.eventStore calendarsForEntityType:EKEntityTypeEvent];
                self.selectedCalendarEventKitIdentifier = [[NSUserDefaults standardUserDefaults] objectForKey:@"eventkit_cal_identifiers_string"];
                for(EKCalendar *aCal in caledars){
                    if([aCal.calendarIdentifier isEqualToString:self.selectedCalendarEventKitIdentifier]){
                        paramCalendar = [self.eventStore calendarWithIdentifier:[[NSUserDefaults standardUserDefaults] objectForKey:@"eventkit_cal_identifiers_string"]];
                    }
                }
                for (EKSource *source in self.eventStore.sources) {
                    if (source.sourceType == EKSourceTypeCalDAV) {
                        paramCalendar.source = source;
                        break;
                    } else if(source.sourceType == EKSourceTypeLocal){
                        paramCalendar.source = source;
                        break;
                    }
                }

        }else{
            return NO;
        }

        /* If a calendar does not allow modification of its contents
         then we can not insert an event into it */
        if (paramCalendar.allowsContentModifications == NO) {
            NSLog (@ "\n\n The selected calendar does not allow modifications.");
            return NO;
        }
        /* Create an event */

        EKEvent * event = [EKEvent eventWithEventStore:paramStore];

        event.calendar = paramCalendar;
        /* Set the properties of the event such as its title,
         start date / time, end date / time, etc. */
        event.title = paramTitle;
        event.notes = paramNotes;
        event.startDate = paramStartDate;
        event.endDate = paramEndDate;
        /* Finally, save the event into the calendar */
        NSError * saveError = nil;
        result = [paramStore saveEvent:event
                                  span:EKSpanThisEvent
                                 error:&saveError];
        if (result == NO) {
            NSLog (@ "\n\n An error occurred = %@", saveError);
        }
        return result;
    }

above code gives following error

上面的代码给出了以下错误

    CalendarCalculations[1668:45103] 
    Error getting shared calendar invitations for entity types 3 from 
    daemon: Error Domain=EKCADErrorDomain Code=1013 
"The operation couldn't be completed. (EKCADErrorDomain error 1013.)"

how can I get rid of it please?

我怎样才能摆脱它?

回答by LembergSun

I had the same problem in my app. I noticed the error occurs when calendarWithIdentifiermethod is called.

我的应用程序中遇到了同样的问题。我注意到calendarWithIdentifier调用方法时发生错误。

EKCalendar *selectedCalendar = [self.eventStore calendarWithIdentifier:selectedCalendarIdentifier];

I don't know exactly why it occurs but I guess it happens when you don't have any shared calendar in your device. Here is a link About shared iCloud calendars

我不知道为什么会发生这种情况,但我猜当您的设备中没有任何共享日历时会发生这种情况。这是关于共享 iCloud 日历的链接

I found the solution hereand i fixed it in my app with next code snippet:

我在这里找到了解决方案并使用下一个代码片段在我的应用程序中修复了它:

EKCalendar *selectedCalendar = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:self.eventStore];
NSString *selectedCalendarIdentifier = [[NSUserDefaults standardUserDefaults] objectForKey:@"selectedCalendarIdentifier"];

//instead of getting calendar by identifier
//get all calendars and check matching in the cycle
NSArray *allCalendars = [self.eventStore calendarsForEntityType:EKEntityTypeEvent];
for (EKCalendar *calendar in allCalendars) {
    if ([calendar.calendarIdentifier isEqualToString:selectedCalendarIdentifier]) {
        selectedCalendar = calendar;
        break;
    }
}

Off course it's not very good way but error disappeared. Hope it will help you ;)

当然,这不是很好的方法,但错误消失了。希望它会帮助你;)

回答by Lisarien

Another cause for this error is to use the bit mask EKEntityMaskEventinstead of the type EKEntityTypeEventfor the entity type parameter creating the calendar.

此错误的另一个原因是使用位掩码EKEntityMaskEvent而不是EKEntityTypeEvent创建日历的实体类型参数的类型。

If you try to create the calendar such as:

如果您尝试创建日历,例如:

EKCalendar *calendar = [EKCalendar calendarForEntityType:EKEntityMaskEvent eventStore:self.eventStore];

Then the calendar is well returned with an identifier, but the calendar seems not to be valid and it cannot be saved although no error is get. No event cannot be added to it. And the calendarWithIdentifierreturns nil.

然后日历很好地返回一个标识符,但日历似乎无效并且无法保存,尽管没有得到错误。不能向其中添加任何事件。并且calendarWithIdentifier返回零。

The right syntax is using EKEntityTypeEventsuch as:

正确的语法是使用EKEntityTypeEvent例如:

EKCalendar *calendar = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:self.eventStore];

回答by Rodrigo Pinto

Old question but, for reference, I had this problem when I switched from working with Reminders to Calendar events. I forgot to change the entity type in all the places.

老问题,但作为参考,当我从使用提醒切换到日历事件时遇到了这个问题。我忘记在所有地方更改实体类型。

Specifically, I was loading the status of my permissions for EKEntityTypeReminderwith

具体来说,我装我的权限的地位EKEntityType提醒

EKAuthorizationStatus authorizationStatus = [EKEventStore authorizationStatusForEntityType:EKEntityTypeReminder];

and latter asking the user permission for EKEntityTypeEventwith

而后者要求EKEntityType用户权限事件

[self.eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
    if (error == nil) {
        // Store the returned granted value.
    } else {
        NSLog(@"%@", [error localizedDescription]);
    }
}];

So, the solution was to change the checking of status of my permissions to this:

因此,解决方案是将我的权限状态检查更改为:

EKAuthorizationStatus authorizationStatus = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];