Alarm Example with Repeat || Reminder Example with Repeat || LocalNotification Repeat || GeoFencing Alarm

Alarm With Repeat daily,weekly or monthly:

In Local Notification repeat will not be workout because iOS had restrictions for app must be 64 notifications.So we can achieve through calender events.

But only one thing is we cannot have to goto the app when user clicks the notification because it will be generated and handled by the calender.

We can schedule an event in the calender using our app and save the event identifier inside our app in DB or Coredata.

After that if you want to edit the event using the identifier is easy.

The below is the example Code:

First you have to import the eventkit in your viewcontroller.Then add the below keys in the info.plist

Privacy - Reminders Usage Description
Privacy - Calendars Usage Description

Then create new event using request access to the event.

First you have to remember that event fromdate and todate are not for multiple days.It will be the event start and end within a single day.So don't confuse that.

If you want to repeat for everyweek for particular days you can set the fromdate as starting date and end date will be in recurrence rule.

if you want an alarm to trigger at time then use EKAlarm.The below example is for from now to next 10days with repeat an event at every monday and friday.Not scheduled at remaining days.



//
//  ViewController.swift
//  reminderExample

import UIKit
import EventKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        var eventDB = EKEventStore.init()
        // Request access to calendar first
        eventDB.requestAccess(to: .event, completion: { (granted, error) in
            if granted {
                var event:EKEvent!
                event = EKEvent.init(eventStore: eventDB)
                let Fromday = Date()
                let Todate = Calendar.current.date(byAdding: .minute, value: 1, to: Fromday)
              let enddate = Calendar.current.date(byAdding: .day, value: 10, to: Fromday)

                event.title = "test"
                event.startDate = Fromday as Date
                event.endDate = Todate
                //
                if event.startDate != event.endDate {
                    let friday = EKRecurrenceDayOfWeek(. friday)
                  let monday = EKRecurrenceDayOfWeek(. monday)

                    let recurrenceRule =
                      EKRecurrenceRule(recurrenceWith: .weekly, interval: 1, daysOfTheWeek:[friday,monday], daysOfTheMonth: nil, monthsOfTheYear: nil, weeksOfTheYear: nil, daysOfTheYear: nil, setPositions: nil, end: EKRecurrenceEnd(end: enddate))
                  //get the count for the repeatation find for all ex - s,m,t,w,t,f - 365 for year
                    event.addRecurrenceRule(recurrenceRule)
                }
                event.calendar = eventDB.defaultCalendarForNewEvents
                
                do {
                    try eventDB.save(event, span: .thisEvent)
                } catch let error as NSError {
                    print("failed to save event with error : \(error)")
                }
            }
            else
            {
                print("calendar not allowed")
            }}
        )
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}





Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Convert NsNumber, NSDate to String in iOS Swift

Global LocationManager Singleton class iOS Swift