Swift uses the Eventkit action to add, modify, delete reminders

Source: Internet
Author: User
Tags bool commit


1, add a reminder

In the example below, fill in the reminder, select the reminder period, and click "Save" to add the reminder to the system's reminder.
(Here I set the inputview of the date input box to Uidatepicker so that when you click on the date text box, the date selector will automatically appear to select the time.) )

Import Uikit
Import Eventkit

Class Viewcontroller:uiviewcontroller {

var eventstore:ekeventstore!
Reminder Message text Box
@IBOutlet weak var remindertextfield:uitextfield!
Reminder Time text Box
@IBOutlet weak var datetextfield:uitextfield!
Reminder Date Picker
var datepicker:uidatepicker!

Override Func Viewdidload () {
Super.viewdidload ()

Self.eventstore = Ekeventstore ()

Set the date selector used by the Reminder time text box
DatePicker = Uidatepicker ()
Datepicker.addtarget (Self, Action: #selector (Viewcontroller.adddate),
forControlEvents:. valuechanged)
Datepicker.datepickermode = Uidatepickermode.dateandtime
Datetextfield.inputview = DatePicker
}

Date Selector Response method
Func adddate () {
Date style
Let formatter = NSDateFormatter ()
Formatter.dateformat = "yyyy year mm month DD Day hh:mm"
Update Reminder time text box
Self.dateTextField.text = Formatter.stringfromdate (datepicker.date)
}

Save Button Response method
@IBAction func Addreminder (sender:anyobject) {
Get access Authorization for reminders
Self.eventStore.requestAccessToEntityType (Ekentitytype.reminder) {
(Granted:bool, Error:nserror?) -> Void in

Create a reminder entry
Let reminder = Ekreminder (EventStore:self.eventStore)
Reminder.title = (self.reminderTextField.text)!
Let duedatecomponents = Self.datecomponentfromnsdate (self.datePicker.date)
Reminder.duedatecomponents = duedatecomponents
Reminder.calendar = Self.eventStore.defaultCalendarForNewReminders ()
Save Reminder Items
do {
Try Self.eventStore.saveReminder (reminder, commit:true)
Print ("Save success!") ")
}catch{
Print ("Create Reminder failed: \ (Error)")
}
}
}

Get the corresponding Nsdatecomponents object according to NSDate
Func datecomponentfromnsdate (date:nsdate)-> nsdatecomponents{
Let Calendarunit:nscalendarunit = [. Minute,. Hour,. Day,. Month,. Year]
Let datecomponents = Nscalendar.currentcalendar (). components (Calendarunit,
Fromdate:date)
Return datecomponents
}

Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()

}
}

2, modify the reminder

The change reminder is similar to the new reminder, we first get to the Reminer object that needs to be modified, and then modify its time or title. The last call to the Eventstore Savereminder method is saved.
The following example modifies all the reminder headers to "Welcome to hangge.com"


Get access Authorization for reminders
Self.eventStore.requestAccessToEntityType (Ekentitytype.reminder) {
(Granted:bool, Error:nserror?) -> Void in

If granted{
After getting the authorization, we can get all the reminder items
Let predicate = self.eventStore.predicateForRemindersInCalendars (nil)
Self.eventStore.fetchRemindersMatchingPredicate (predicate, completion: {
(Reminders: [Ekreminder]?) -> Void in

Iterate through all reminders and modify
For reminder in reminders! {
Reminder.title = "Welcome to visit Hangge.com"
Reminder.calendar = Self.eventStore.defaultCalendarForNewReminders ()
Save Reminder Items
do {
Try Self.eventStore.saveReminder (reminder, commit:true)
Print ("Save success!") ")
}catch{
Print (Save failed: \ (Error))
}
}
})
}else{
Print ("Get Reminder failed!") Authorization is required to allow access to reminder matters. ")
}
}

3, delete reminders
We first get to the Reminer object that needs to be deleted, and then call the Eventstore Removereminder method.
The following sample deletes all reminders in the system.

Get access Authorization for reminders
Self.eventStore.requestAccessToEntityType (Ekentitytype.reminder) {
(Granted:bool, Error:nserror?) -> Void in

If granted{
After getting the authorization, we can get all the reminder items
Let predicate = self.eventStore.predicateForRemindersInCalendars (nil)
Self.eventStore.fetchRemindersMatchingPredicate (predicate, completion: {
(Reminders: [Ekreminder]?) -> Void in

Iterate through all reminders and delete
For reminder in reminders! {
Save Reminder Items
do {
Try Self.eventStore.removeReminder (reminder, commit:true)
Print ("Delete succeeded!") ")
}catch{
Print ("Delete failed: \ (Error)")
}
}
})
}else{
Print ("Get Reminder failed!") Authorization is required to allow access to reminder matters. ")
}
}

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.