iOS Calendar related actions--read system calendar, add event to System calendar

Source: Internet
Author: User
Tags dateformat

1 reading the system calendar

Let Eventstore =Ekeventstore () let Tempformatter=NSDateFormatter () Tempformatter.dateformat="DD. MM.YYYY hh:mm"                //Get calendar events in a time periodLet StartDate = tempformatter.datefromstring ("10.08.2016 15:10")!Let endDate= Tempformatter.datefromstring ("12.08.2016 15:30")!Let predicate=eventstore.predicateforeventswithstartdate (startdate, enddate:enddate, calendars: [ Eventstore.defaultcalendarfornewevents])//get all the schedules in this time periodLet events =eventstore.eventsmatchingpredicate (predicate)

2 adding events to the system calendar

     Let Eventstore = Ekeventstore ()
Eventstore.requestaccesstoentitytype (. Event) {(Granted, error)inchDispatch_async (Dispatch_get_main_queue (), {ifError! =Nil {//error occurred}Else if!granted {//do not allow access to calendars}Else { //Create EventLetEvent=ekevent (Eventstore:eventstore)Event. title ="Lallallallal"Let tempformatter=NSDateFormatter () Tempformatter.dateformat="DD. MM.YYYY hh:mm" //Create a calendar event for a time period Event. StartDate = Tempformatter.datefromstring ("11.08.2016 15:20")!Event. endDate = Tempformatter.datefromstring ("11.08.2016 15:30")!//set whether to be an all-day event Event. AllDay =false //Set reminder time for events (relative time) 15 minutes early reminder Event. Addalarm (Ekalarm (relativeoffset:-60.0*15.0)) //Set reminder time for an event (absolute time)//Event.addalarm (Ekalarm (Absolutedate:nsdate (timeinterval: -60 *, SinceDate:event.startDate))) Event. Calendar =eventstore.defaultcalendarfornewevents//Save events, add to Calendar Do { TryEventstore.saveevent (Event, Span:. Thisevent, Commit:true)}Catch { } } }) }

3 Analysis:

Calendars are divided into two categories, a class of calendars for storing events, and a class of calendars for storing reminders.

The Eventkit Library framework grants access to the user's Calendar app and reminders app. Although two different apps are used to display the user's calendar and reminder data, it is the same framework that maintains the data. Similarly, the database that stores this data is called a calendar database, which accommodates both calendar and reminder messages.

The event library not only allows your app to get calendar and reminder data that the user already has, but it lets your app create new events and reminders for any calendar. In addition, the event library allows users to edit and delete their events and reminders (collectively called "Calendar Items"). If the calendar database has changes from outside your app, the event library can be monitored by notification so your app can respond appropriately. Changes that you make to calendar items by using the event library are automatically synced to the relevant calendar.

(1) Read and write calendar database

You can use EKEventStore classes to get, create, edit, and delete events from the user's calendar database. You can get a custom set of events that match the events of the predicate that you provide, or get a separate event by a unique identity. After you obtain an event, you can use the EKEvent properties of the class to get the calendar information related to accessing the event. Similarly, you can EKEvent modify the calendar information for the event by setting the properties of the class.

  • Connect to Event Library
    In IOS 5 and later versions of the system, the default initializer is used:
    Let Eventstore = Ekeventstore ()

    In IOS 6 and later, you must use a requestAccessToEntityType:completion: method request to use the user's calendar database after the event library is initialized. Requesting access to an entity type asynchronously prompts the user to allow or disallow your app to use their calendar information. You should deal with the various conditions in which users authorize or prohibit access to your app:

    Inch

    }

    EKEventStoreObjects require a relatively large amount of time to initialize and release. Therefore, you should not initialize and release the event library for each event-related task. Instead, initialize an event library when your app loads, and then use this one repeatedly to ensure that the connection is always available. Event Library instances should not be freed before other event development package objects are released, or unexpected states may occur.

  • Get Event
    There are two ways of getting events. obtained by a predicate or search query returns 0 or more events that match a given query. Gets a unique event that matches the given identity by using a unique identity.
    Note : Getting events from the calendar database does not necessarily return in chronological order. To sort EKEvent an array of objects by date, you can call the method on the array sortedArrayUsingSelector: and provide a compareStartDateWithEvent: selector for the method.
  • The
  • use predicate
    is typically to get events that belong to a range of dates.   ekeventstore   eventsmatchingpredicate:   method gets all events that belong to the date range specified in the predicate that you provide.
    note : although    eventsmatchingpredicate:   The method accepts a   nspredicate type parameter, but you must provide a method   with   ekeventstore  ; predicateforeventswithstartdate:enddate:calendars:  the verb created by the .
    You can specify a subset of the calendar to search for, which requires passing a   Ekcalendar   An array of objects as   PredicateForEventsWithStartDate:endDate:calendars: The   of the   method; calendars   parameters. You can get the different types of calendars for the user from the event library's   Calendarsforentitytype:   method. If you pass a nil value, you are telling this method to get all the users ' calendars.
    because method    eventsmatchingpredicate:   is synchronous, and you may not want to run it in the main thread of your app. If you want to execute asynchronously, use   dispatch_async
      function or use a   Nsoperation the   object, you can run the method in another thread.
  • Using unique identities
    If you have previously used a predicate to get an event and know its unique identity, then you can use EKEventStore the eventWithIdentifier: method to get the event again. If it is a looping event, then this method returns the event that occurred for the first time. You can use attributes to eventIdentifier obtain a unique identifier for an event.
  • Create and edit events
    Use EKEvent the Eventwitheventstore: Method of the event to create a new event.
  • Save and remove events

    Tip : If your app modifies the user's calendar database, it must get confirmation from the user before this is done. It is never possible for an app to modify the calendar database without getting specific instructions from the user.

    Your changes to the events are not persisted until you save them. Use EKEventStore the SaveEvent:span:commit:error: method to save your changes to the calendar database. If you want to remove events from the Calendar database, use the Ekeventstore RemoveEvent:span:commit:error: method. Regardless of whether you save or remove events, the respective implemented methods are automatically modified to the calendar that the event belongs to (CalDav, Exchange, and so on).

    If you save a looping event, you can saveEvent:span:commit:error: EKSpanFutureEvents make your changes apply to all future occurrences by assigning a parameter span designation to the method. Similarly, you can also specify a removeEvent:span:commit:error: method's span parameter value to EKSpanFutureEvents remove all future occurrences of an event.

    Note : If you pass the NO value to the commit parameter, make sure that you call the commit: method to persist your changes (the translator notes: Default YES will persist the changes immediately).

Reference Link: http://www.cnblogs.com/xiaobaichangan/p/5160025.html

iOS Calendar related actions--read system calendar, add event to System calendar

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.