I have written an article about how to operate on System calendar events (event) (Source address: Swift-use Eventkit to get System calendar events, add events)
Next, I'll show you how to use Eventkit to work with reminders (reminder) in your system, and this article first describes how to get all the reminders in the system.
1, Effect chart
After the program is started, all reminders are loaded and displayed in the table. (Access authorization is required for the first startup)
2, sample code
Import Uikit
Import Eventkit
Class Viewcontroller:uiviewcontroller, Uitableviewdelegate, Uitableviewdatasource {
var eventstore:ekeventstore!
var reminders: [ekreminder]!
var tableview:uitableview?
Override Func Viewdidload () {
Super.viewdidload ()
Create a table view
Self.tableview = UITableView (Frame:self.view.frame, style:. Plain)
self.tableview!. delegate = Self
self.tableview!. DataSource = Self
Self.view.addSubview (self.tableview!)
Before you get a reminder, you need to get the authorization
Self.eventstore = Ekeventstore ()
Self.reminders = [Ekreminder] ()
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
Self.reminders = Reminders
Print (Self.reminders.count)
Dispatch_async (Dispatch_get_main_queue ()) {
Self.tableview? Reloaddata ()
}
})
}else{
Print ("Get Reminder failed!") Authorization is required to allow access to reminder matters. ")
}
}
}
In this case, there is only one partition
Func Numberofsectionsintableview (Tableview:uitableview)-> Int {
return 1;
}
Returns the number of table rows (that is, returns the number of controls)
Func TableView (Tableview:uitableview, numberofrowsinsection section:int)-> Int {
Return Self.reminders.count
}
Create each cell display (create a parameter indexpath a specified cell)
Func TableView (Tableview:uitableview, Cellforrowatindexpath Indexpath:nsindexpath)
-> UITableViewCell
{
Let cell = UITableViewCell (Style:UITableViewCellStyle.Subtitle,
Reuseidentifier: "MyCell")
Let reminder:ekreminder! = self.reminders! [Indexpath.row]
Content of Reminders
Cell.textlabel? Text = Reminder.title
Time to remind matters
Let Formatter:nsdateformatter = NSDateFormatter ()
Formatter.dateformat = "Yyyy-mm-dd"
If let DueDate = reminder.duedatecomponents? date{
Cell.detailtextlabel? Text = Formatter.stringfromdate (duedate)
}else{
Cell.detailtextlabel? Text = "N/a"
}
return cell
}
Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
}
}