如何在iOS设备上创建新的EKCalendar? [英] how do I create a new EKCalendar on iOS device?

查看:929
本文介绍了如何在iOS设备上创建新的EKCalendar?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我想安排一些活动。所以我想为我的应用创建一个新的日历,如果它还不存在,并且在添加新事件时确实引用了它。

I have an app where I want to schedule some events. So I want to create a new calendar for my app if it does not yet exist and if it does reference that when adding new events.

推荐答案

这是在iOS 5上使用 EventKit 框架完成的方式:

This is how it is done on iOS 5 using the EventKit framework:

首先你需要一个 EKEventStore 对象来访问所有内容:

First of all you need an EKEventStore object to access everything:

EKEventStore *store = [[EKEventStore alloc] init];

现在,如果要将日历存储在本地,则需要查找本地日历源。还有交换账户,CALDAV,MobileMe等来源:

Now you need to find the local calendar source, if you want the calendar to be stored locally. There are also sources for exchange accounts, CALDAV, MobileMe etc.:

// find local source
EKSource *localSource = nil;
for (EKSource *source in store.sources)
    if (source.sourceType == EKSourceTypeLocal)
    {
        localSource = source;
        break;
    }

现在,您可以在此处获取以前创建的日历。创建日历(见下文)时,会有一个ID。创建日历后必须存储此标识符,以便您的应用可以再次识别日历。在这个例子中,我只是将标识符存储在一个常量中:

Now here's the part where you can fetch your previously created calendar. When the calendar is created (see below) there is an ID. This identifier has to be stored after creating the calendar so that your app can identify the calendar again. In this example I just stored the identifier in a constant:

NSString *identifier = @"E187D61E-D5B1-4A92-ADE0-6FC2B3AF424F";

现在,如果您还没有标识符,则需要创建日历:

Now, if you don't have an identifier yet, you need to create the calendar:

EKCalendar *cal;
if (identifier == nil)
{
    cal = [EKCalendar calendarWithEventStore:store];
    cal.title = @"Demo calendar";
    cal.source = localSource;
    [store saveCalendar:cal commit:YES error:nil];
    NSLog(@"cal id = %@", cal.calendarIdentifier);
}

您还可以配置日历颜色等属性。重要的部分是存储标识符供以后使用。
另一方面,如果您已经拥有标识符,则只需获取日历:

You can also configure properties like the calendar color etc. The important part is to store the identifier for later use. On the other hand if you already have the identifier, you can just fetch the calendar:

else
{
    cal = [store calendarWithIdentifier:identifier];
}

我也输入了一些调试输出:

I put in some debug output as well:

NSLog(@"%@", cal);

现在你要么有一个 EKCalendar 对象进一步使用。

Now you either way have a EKCalendar object for further use.

编辑:从iOS 6 calendarWithEventStore 折旧后,使用:

As of iOS 6 calendarWithEventStore is depreciated, use:

cal = [EKCalendar calendarForEntityType:<#(EKEntityType)#> eventStore:<#(EKEventStore *)#>];

这篇关于如何在iOS设备上创建新的EKCalendar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆