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

标签 ios objective-c ios6 calendar

我在创建 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);
}

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

- (IBAction)two:(id)sender {

NSArray *calendars = [eventStore calendarsForEntityType:EKEntityTypeEvent];

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

}

提前谢谢您!

最佳答案

我找到了解决方案。问题是,当 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;
            }
        }
    }

关于ios - 如何在 ios 6 上创建和保存 EKCalendar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41775899/

相关文章:

ios - 尝试圆形图像,输出菱形?

objective-c - 自定义 iOS UILocalNotification 声音不播放(可能与 Xcode 更新有关)

ios - 使用分页从 NSArray 创建多个 UIButton

iphone - 获取被触摸的按钮的标题

iphone - cocos2d如何根据不同的iOS设备加载不同的图片?

objective-c - 随机移动 - Windows Pipe Screensaver

ios - 将 TableView Controller 中的单元格链接到网页

iphone - 语义问题 - Xcode 实现不完整

iphone - 核心数据不同提取给出空数组

iphone - 模态呈现的 TableView 没有导航栏