如何在ios 6上创建和保存EKCalendar

tom*_*nas 14 calendar objective-c ios ios6

我有一个问题,我创建我的EKCalendar,一切看起来不错,但是当我去列出我的日历时,它没有出现.我也去我的日历应用程序查看我的日历列表,但它不存在.有什么想法吗?

这是我创建日历的按钮代码:

- (IBAction)one:(id)sender {
NSString* calendarName = @"My Cal";
EKCalendar* calendar;

// Get the calendar source
EKSource* localSource;
for (EKSource* source in eventStore.sources) {
    if (source.sourceType == EKSourceTypeLocal)
    {
        localSource = source;
        break;
    }
}

if (!localSource)
    return;

calendar = [EKCalendar calendarWithEventStore:eventStore];
calendar.source = localSource;
calendar.title = calendarName;

NSError* error;
bool success= [eventStore saveCalendar:calendar commit:YES error:&error];
if (error != nil)
{
    NSLog(error.description);
    // TODO: error handling here
}
NSLog(@"cal id = %@", calendar.calendarIdentifier);
}
Run Code Online (Sandbox Code Playgroud)

这是我的按钮代码列出日历,但我的新日历从未包括在内!

- (IBAction)two:(id)sender {

NSArray *calendars = [eventStore calendarsForEntityType:EKEntityTypeEvent];

for (EKCalendar* cal in calendars){
    NSLog(@"%@",cal.title);
}

}
Run Code Online (Sandbox Code Playgroud)

先感谢您!

Mas*_*ero 35

我找到了解决方案.问题是,当iCloud日历打开时,它会从日历应用程序中隐藏本地创建的日历.要绕过此问题,解决方案是向iCloud源添加新日历:

    for (EKSource *source in self.eventStore.sources)
    {
        if (source.sourceType == EKSourceTypeCalDAV && 
           [source.title isEqualToString:@"iCloud"]) //Couldn't find better way, if there is, then tell me too. :)
        {
            localSource = source;
            break;
        }
    }

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


CHi*_*-NY 6

我也有这个问题.我的解决方案几乎与其他答案一样,但我使用另一种方式来获取EKSource的实例.

如文档中所述:

/* 
 * Returns the calendar that events should be added to by default, 
 * as set in the Settings application.
 */
@property EKCalendar *defaultCalendarForNewEvents; 
Run Code Online (Sandbox Code Playgroud)

所以,我使用这段代码来获得正确的EKSource:

EKSource *theSource = [self.eventStore defaultCalendarForNewEvents].source;
Run Code Online (Sandbox Code Playgroud)

但是,如果您使用其他日历(如Gmail(或Yahoo等)),则无法将日历添加到其来源.

在这种罕见的情况下,我使用完整搜索:

EKSource *theSource;
for (EKSource *source in eventStore.sources) {
    if (source.sourceType == EKSourceTypeSubscribed) {
        theSource = source;
        break; // stop when source is found
    }
}
Run Code Online (Sandbox Code Playgroud)