Make Calendar Event Reminder App Using Event Kit Framework

How to Make Calendar Event Reminder App Using Event Kit Framework?

0
Shares
How to Make Calendar Event Reminder App Using Event Kit Framework?
1 (20%) 1 vote

Make Calendar Event Reminder App Tutorial

To iOS Developers,

Can you set reminders on your phone without using the Reminder app?

No?

Think Again!

Umm… Not Sure?

Ok, let’s clear it.

Yes, you can absolutely create events and set reminders without using a Reminder app.

With Event Kit Framework, it is possible.

Through this tutorial, you’ll see the entire process of how to make calendar event reminder app.

The main purpose of this framework is to provide access to reminder and calendar app. Because behind these both apps, there is the same database to store the information.

And with this framework, you can create, update, delete both, Events as well as Reminders directly through your iOS app.

In this iOS app tutorial, we’ll see how to implement the Event Kit Framework, how to get the access permission, and making a calendar event reminder in iOS app.

Let’s Get Started

Create a new project under the file menu and select “Single View Application”.

Calender1

In the next tab, name your project demo and click on finish. Here, we’re naming it as SOCalendarEvent.

Calender2

Now import EventKit and set a static date formatter in “ViewController.swift”

Next, Set outlet of text field and buttons

To fetch events from the calendar, we have to get permission first, call this method from viewDidLoad() in “ViewController.swift”

override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

        self.SOGetPermissionCalendarAccess()

    }

    //MARK: Get Premission for access Calender

    func SOGetPermissionCalendarAccess() {

        switch EKEventStore.authorizationStatusForEntityType(.Event) {

        case .Authorized:

            print("Authorised")

        case .Denied:

            print("Access denied")

        case .NotDetermined:

            // 3

            eventStore.requestAccessToEntityType(.Event, completion: { (granted: Bool, error:NSError?) -> Void in

                if granted {

                    print("Granted")

                } else {

                    print("Access Denied")

                }

            })

        default:

            print("Case Default")

        }

    }

Calender3

Once user allows accessing calendar events, then we will select the start date and end date by date picker and navigate to EventVC.swift class with start and end date.

Call getAllEvents() method in viewDidLoad() of “EventVC.swift” class and save all the data in Array.

let arrEvent = NSMutableArray()

    @IBOutlet weak var tblEvent: UITableView!

    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view.

        self.getAllEvents()

    }

    func getAllEvents() {

        

        let eventStore = EKEventStore()

        let calendars = eventStore.calendarsForEntityType(.Event)

        

        for calendar in calendars {

            print(calendar)

            

            let dateFormatter = NSDateFormatter()

            dateFormatter.dateFormat = setDateFormat

            

            let dateStart = dateFormatter.dateFromString(startDate)!

            let dateEnd = dateFormatter.dateFromString(endDate)!

            

            let predicate = eventStore.predicateForEventsWithStartDate(dateStart, endDate: dateEnd, calendars: [calendar])

            

            let events = eventStore.eventsMatchingPredicate(predicate)

            

            for event in events {

                let dict = NSMutableDictionary()

                dict.setValue(event.title, forKey: "title")

                dict.setValue(event.startDate, forKey: "startDate")

                dict.setValue(event.endDate, forKey: "endDate")

                dict.setValue(calendar.title, forKey: "type")

                arrEvent.addObject(dict)

            }

        }

        tblEvent.reloadData()

        print(arrEvent)

    }

Once all events are stored in “arrEvent”, we’ll reload the table for showing all the events.

//MARK: UITableViewDataSource

    

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {

        return 1

    }

    

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return arrEvent.count

    }

    

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

        configureCell(cell, forRowAtIndexPath: indexPath)

        return cell

    }

    

    func configureCell(cell: UITableViewCell, forRowAtIndexPath: NSIndexPath) {

        let dict = arrEvent.objectAtIndex(forRowAtIndexPath.row) as! NSDictionary

        

        let lblName = cell.contentView.viewWithTag(1) as! UILabel

        lblName.text = dict.valueForKey("title") as? String

        

        let lblType = cell.contentView.viewWithTag(2) as! UILabel

        lblType.text = dict.valueForKey("type") as? String

        

        let lblStartDate = cell.contentView.viewWithTag(3) as! UILabel

        lblStartDate.text = self.convertDateIntoString((dict.valueForKey("startDate") as? NSDate)!)

        

        let lblEndDate = cell.contentView.viewWithTag(4) as! UILabel

        lblEndDate.text = self.convertDateIntoString((dict.valueForKey("endDate") as? NSDate)!)

    }

    

    func convertDateIntoString(dateString : NSDate) -> String {

        let dateFormatter = NSDateFormatter()

        dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss +zzz"

        let dateObj = dateFormatter.stringFromDate(dateString)

        return dateObj

    }

    //MARK: UITableViewDelegate

    

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        

    }

Now for adding an event in calendar set outlet of the text field for start date, end date, and event name.

Calender4

Once user enters all three inputs, add events by calling following method on button tap event.

@IBAction func actionAddEvent(sender: AnyObject) {

        if txtStartDate.text == "" || txtEndDate.text == "" || txtEventName.text == "" {

            return

        }

        

        let dateFormatter = NSDateFormatter()

        dateFormatter.dateFormat = setDateFormat

        let dateStart = dateFormatter.dateFromString((txtStartDate.text)!)

        let dateEnd = dateFormatter.dateFromString((txtEndDate.text)!)

        

        let event:EKEvent = EKEvent(eventStore: eventStore)

        event.title = txtEventName.text!

        event.startDate = dateStart!

        event.endDate = dateEnd!

        event.notes = "This is a note"

        event.calendar = eventStore.defaultCalendarForNewEvents

        

        do {

            try eventStore.saveEvent(event, span: .ThisEvent)

            print("events added with dates:")

            self.actionBack(sender)

        } catch let e as NSError {

            print(e.description)

            return

        }

        print("Saved Event")

        

    }

Done!

As you can see, working with Event Kit Framework is quite a simple process, nothing special is needed. And adding such calendar features in your iOS app can also increase the interaction in your mobile app.

However, if you face any problem or need technical assistance to implement this framework in your iOS app, Hire iPhone app developer from us.

Grab a free copy of calendar event reminder app demo from Github.

 
0
Shares
 

Any Questions? Want to Develop iOS App? Contact Us Now.

Get your free consultation now