Hey guys I am a beginner programmer making my first app. I am trying to create a "note to future self" IOS app on Xcode. Basically you add a note that you want to share with yourself at any particular date in the future. Does anyone have any suggestions as to how I can implement this either by using Eventkit or by some other method?
1 Answers
You can do it like this, here is the play by play:
You have to add NSCalendarsUsageDescription key in your plist file, because you are accessing user's privacy sensitive data
Go to your ViewController, import EventKit
Add this code on your viewDidLoad on any viewcontroller to add event to device calendar, you can move this code later based on how you gonna build your app
let eventStore = EKEventStore()
let event = EKEvent(eventStore: eventStore)
event.title = "My First Event"
event.startDate = NSDate() as Date
event.endDate = event.startDate.addingTimeInterval(60*60)
event.calendar = eventStore.defaultCalendarForNewEvents
do{
try eventStore.save(event, span: .thisEvent, commit: true)
}catch let err{
print("Error: \(err.localizedDescription)")
}
Here's what we do
we made an object from EKEventStore class, EKEventStore is an application’s point of contact for accessing calendar and reminder data. You can read more about it here EKEventStore
we made an event object, set its name as "My First Event", its startDate as current date, its end date as current time plus 2 hours, and eventually its calendar to default calendar set on user's device
then we try to insert the event object we just made to device calendar, and voila
remember to wrap save function in a do catch block since it could throw error
Welcome to SO, next time please provide more explanation about your question, could be what have you done or what have gone wrong doing it :D

- 30,962
- 25
- 85
- 135

- 174
- 1
- 8
-
thank you so much! Yes next time I will provide more information this was just to get a sense. – Aryaman Darda Oct 21 '20 at 06:14