Skip to content

Instantly share code, notes, and snippets.

@genedelisa
Created June 30, 2014 23:24
Show Gist options
  • Save genedelisa/f9d350f0ac90cc079f3d to your computer and use it in GitHub Desktop.
Save genedelisa/f9d350f0ac90cc079f3d to your computer and use it in GitHub Desktop.
Create a calendar event with alarm
var store:EKEventStore = EKEventStore()
store.requestAccessToEntityType(EKEntityTypeEvent,
completion:{(granted:Bool, error:NSError?) -> Void in
if let e = error {
println("Error \(e.localizedDescription)")
}
if granted {
println("access granted")
var date:NSDate = NSDate.date()
var event:EKEvent = EKEvent(eventStore: store)
event.title = "Someone's birthday"
event.startDate = date
event.endDate = date
event.allDay = true
event.calendar = store.defaultCalendarForNewEvents
// 60 seconds before
var alarm:EKAlarm = EKAlarm(relativeOffset: -60)
event.alarms = [alarm]
var err:NSError?
store.saveEvent(event, span:EKSpanThisEvent, error:&err)
if let se = err {
println("could not save \(se.localizedDescription)")
}
}
})
@carlosnorena
Copy link

thanks for coding this ,,, here is the iOS9 swift 2 update

cheers from colombia Carlos Norena

here its the code

let store:EKEventStore = EKEventStore()

    store.requestAccessToEntityType(EKEntityType.Event) { (granted, error) -> Void in

            if let e = error {
                print("Error \(e.localizedDescription)")
            }

            if granted {
                print("access granted")

                let date : NSDate = NSDate()
                let event:EKEvent = EKEvent(eventStore: store)
                event.title = "Someone's birthday"
                event.startDate = date
                event.endDate = date
                event.allDay = true
                event.calendar = store.defaultCalendarForNewEvents

                // 60 seconds before
                let alarm:EKAlarm = EKAlarm(relativeOffset: -60)
                event.alarms = [alarm]



                do {

                try store.saveEvent(event, span: EKSpan.ThisEvent, commit: true)

                } catch {

                    //let err:NSError?
                    print(error)
                }
            }
    }

@TamilRaja
Copy link

TamilRaja commented Apr 17, 2017

How to update the event? and get alarm offset value?

@dipakviprak
Copy link

dipakviprak commented Apr 4, 2018

how to create my app name calendar using swift ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment