Swift uses the Eventkit action "reminders" to query all reminders

Source: Internet
Author: User

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 ()
}
}

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.