美文网首页
读写日历事件 Reading and Writing Calen

读写日历事件 Reading and Writing Calen

作者: 翻滚的炒勺2013 | 来源:发表于2017-01-10 15:30 被阅读331次

    您可以使用EKEventStore类从用户的日历数据库中提取,创建,编辑和删除事件。 您可以提取符合您提供的谓词的自定义事件集,也可以通过其唯一标识符获取单个事件。 获取事件后,您可以使用EKEvent类的属性访问其关联的日历信息。 同样,您可以通过设置EKEvent类的属性来修改其日历信息。

    You can fetch, create, edit, and delete events from a user’s Calendar database using the EKEventStore class. You can fetch a custom set of events that match a predicate you provide, or you can fetch an individual event by its unique identifier. After you fetch an event, you can access its associated calendar information with the properties of the EKEvent class. Likewise, you can modify its calendar information by setting the properties of the EKEvent class.

    重要提示:如果您的iOS应用在iOS 10.0或更高版本上链接,并且您需要访问日历数据,请务必在Info.plist文件中包含NSCalendarsUsageDescription键。

    Important: If your iOS app links on or after iOS 10.0 and you need to access Calendar data, be sure to include the NSCalendarsUsageDescription key in your Info.plist file.

    连接到事件存储

    Connecting to the Event Store

    您使用指定的初始化程序初始化一个EKEventStore对象
    You initialize an EKEventStore object with the designated initializer:

    EKEventStore *store = [[EKEventStore alloc] initWithAccessToEntityTypes:EKEntityMaskEvent];
    

    EKEventStore对象需要相对大量的时间来初始化和释放。 因此,不应为每个与事件相关的任务初始化和释放单独的事件存储。 相反,应用程序加载时初始化单个事件存储,并重复使用它以确保您的连接是长期的。

    An EKEventStore object requires a relatively large amount of time to initialize and release. Consequently, you should not initialize and release a separate event store for each event-related task. Instead, initialize a single event store when your app loads, and use it repeatedly to ensure that your connection is long-lived.

    事件存储实例不能在其他EventKit对象之前释放; 否则,可能会出现未定义的行为。
    An event store instance must not be released before other EventKit objects; otherwise, undefined behavior may occur.

    检索事件
    Retrieving Events

    有两种方式检索事件。 通过谓词或搜索查询获取,将返回与给定查询匹配的零个或多个事件。 通过唯一标识符获取将返回与给定标识符相对应的单个事件。

    There are two ways to retrieve events. Fetching via predicates, or search query, will return zero or more events that match a given query. Fetching via unique identifiers will return a single event that corresponds to the given identifier.

    注意:从日历数据库检索事件不一定按时间顺序返回事件。 要按日期对EKEvent对象的数组排序,请在数组上调用sortedArrayUsingSelector:,为compareStartDateWithEvent:方法提供选择器。

    Note: Retrieving events from the Calendar database does not necessarily return events in chronological order. To sort an array of EKEvent objects by date, call sortedArrayUsingSelector: on the array, providing the selector for the compareStartDateWithEvent: method.

    使用谓词
    Using Predicates

    提取属于日期范围内的事件通常很常见。 EKEventStore方法eventsMatchingPredicate:获取所提供的谓词中指定的日期范围内的所有事件。 清单1-1演示了如何获取在当前日期之前一天和一年之后发生的所有事件。

    It’s common to fetch events that fall within a date range. The EKEventStore method eventsMatchingPredicate: fetches all events that fall within the date range specified in the predicate you provide. Listing 1-1 demonstrates how to fetch all events that occur between one day before and one year after the current date.

    注意:尽管eventsMatchingPredicate:方法接受类型为NSPredicate的参数,但必须提供使用EKEventStore方法predicateForEventsWithStartDate:endDate:calendars:创建的谓词。

    Note: Although the eventsMatchingPredicate: method accepts a parameter of type NSPredicate, you must supply a predicate created with the EKEventStore method predicateForEventsWithStartDate:endDate:calendars:.

    清单1-1使用谓词获取事件
    Listing 1-1 Fetching events with a predicate

    // 获取适当的日历
    NSCalendar *calendar = [NSCalendar currentCalendar];
     
    // 创建开始日期组件
    NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init];
    oneDayAgoComponents.day = -1;
    NSDate *oneDayAgo = [calendar dateByAddingComponents:oneDayAgoComponents
                                                  toDate:[NSDate date]
                                                 options:0];
     
    // 创建结束日期组件
    NSDateComponents *oneYearFromNowComponents = [[NSDateComponents alloc] init];
    oneYearFromNowComponents.year = 1;
    NSDate *oneYearFromNow = [calendar dateByAddingComponents:oneYearFromNowComponents
                                                       toDate:[NSDate date]
                                                      options:0];
     
    // 创建一个从事件存储的实例方法谓语
    NSPredicate *predicate = [store predicateForEventsWithStartDate:oneDayAgo
                                                            endDate:oneYearFromNow
                                                          calendars:nil];
     
    // 获取与谓词匹配的所有事件
    NSArray *events = [store eventsMatchingPredicate:predicate];
    

    相关文章

      网友评论

          本文标题:读写日历事件 Reading and Writing Calen

          本文链接:https://www.haomeiwen.com/subject/qzwkbttx.html