objective-c - Objective C-iCal 不在 iOS 9 中为其创建自定义日历和新事件

标签 objective-c iphone ios8 ios9 icalendar

这在 iOS 8 中完美运行。

但在 iOS 9 中创建问题。这是代码:

self.eventManager.eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
        if (granted) {

            // Create a new calendar.
            EKCalendar *calendar = [EKCalendar calendarForEntityType:EKEntityTypeEvent
                                                          eventStore:self.eventManager.eventStore];

            // Set the calendar title.
            calendar.title = @"<APP name>";
            calendar.CGColor=APP_Blue_COLOR.CGColor;

            // Find the proper source type value.
            for (int i=0; i<self.eventManager.eventStore.sources.count; i++) {
                EKSource *source = (EKSource *)[self.eventManager.eventStore.sources objectAtIndex:i];
                EKSourceType currentSourceType = source.sourceType;

                if (currentSourceType == EKSourceTypeLocal) {
                    calendar.source = source;
                    break;
                }
            }


            // Save and commit the calendar.
            NSError *error;
            [self.eventManager.eventStore saveCalendar:calendar commit:YES error:&error];

            // If no error occurs then turn the editing mode off, store the new calendar identifier and reload the calendars.
            if (error == nil) {
                // Turn off the edit mode.
                // Store the calendar identifier.
                [self.eventManager saveCustomCalendarIdentifier:calendar.calendarIdentifier];self.eventManager.selectedCalendarIdentifier=calendar.calendarIdentifier;//chirag

            }
            else{
                // Display the error description to the debugger.
                NSLog(@"CREATE_CALENDER %@", [error localizedDescription]);
            }

        }
        else
        {

            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"" message:@"Please give permission to access your iPhone calender." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
            [alert show];


        }

    }];

它给我成功消息,但没有在 iPhone 日历中创建我的应用程序日历。

我虽然由于没有设置事件而没有显示它。所以我也尝试设置新事件。

但它在创建新事件时给了我以下代码和错误。

// Create a new event object.
    EKEvent *event = [EKEvent eventWithEventStore:self.eventManager.eventStore];

    // Set the event title.
    event.title = title;

    // Set its calendar.
    event.calendar = [self.eventManager.eventStore calendarWithIdentifier:self.eventManager.selectedCalendarIdentifier];

    // Set the start and end dates to the event.
    event.startDate = startDate;
    event.endDate = endDate;
 // Save and commit the event.
    NSError *error;
    if ([self.eventManager.eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&error]) {
        // Call the delegate method to notify the caller class (the ViewController class) that the event was saved.
        return true;
    }
    else{
        // An error occurred, so log the error description.
        NSLog(@"%@", [error localizedDescription]);
    return false;
    }

它在内部给出以下错误,但它会在 NSError 对象中返回:

Error getting shared calendar invitations for entity types 3 from daemon: Error Domain=EKCADErrorDomain Code=1014 "(null)"

最佳答案

问题是当 iCloud 日历打开时,它会从日历应用程序中隐藏本地创建的日历。要绕过这个问题,解决方案是向 iCloud 源添加一个新日历:

for (EKSource *source in self.eventStore.sources)
{
    if (source.sourceType == EKSourceTypeCalDAV && 
       [source.title isEqualToString:@"iCloud"]) //This is a patch.
    {
        localSource = source;
        break;
    }
}

if (localSource == nil)
{
    for (EKSource *source in self.eventStore.sources)
    {
        if (source.sourceType == EKSourceTypeLocal)
        {
            localSource = source;
            break;
        }
    }
}

关于objective-c - Objective C-iCal 不在 iOS 9 中为其创建自定义日历和新事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33258997/

相关文章:

iOS 无法从不同的 View Controller 向按钮添加操作

ios - 轻量级迁移时我的应用程序崩溃

iphone - 如何在 iPhone 中运行应用程序时处理电话

ios - 如何让 gif 在 UIWebView 中永不停止?

objective-c - NSView 中的放置位置

iphone - 如何在不共享 iPhone/iPad 源代码的情况下将我们的应用程序代码集成到其他应用程序中?

iphone - 任何提供 JSON/XML 穆斯林祈祷时间的服务器?

swift - SWRevealViewController 和 Swift 的正确使用方法是什么?

ios - UIAlertController 状态栏没有变暗和色调问题

iphone - 构建失败 : symbol(s) not found