I'm wondering if there is someone who knows how to launch calendar with a specific event from APP
I've done some research and I've come up with two ways to open native calendar from inside the app using NSURL
"calshow://"
which opens calendar at current date
"calshow:\(someNSDate.timeIntervalSinceReferenceDate)"
which opens calendar with date of someNSDate
I also found this website that lists calshow:x?eventid=id
as url but I'm not sure if this works (listed as not public) and I couldn't get it working myself, tried using :
event.calendarItemExternalIdentifier
event.eventIdentifier
event.calendarItemIdentifier
currently I'm using this code to open the calendar app at the finalInterval date, date of the event
if let day = hackathon?.start {
let today = NSDate()
let timeSince = NSDate.timeIntervalSinceReferenceDate() // this plus
let todayToFutureDate = day.timeIntervalSinceDate(today)
let finalInterval = todayToFutureDate + timeSince
UIApplication.sharedApplication().openURL(NSURL(string: "calshow:\(finalInterval)")!)
}
What I'd like to do is to open up the calendar with an event id or something like that that would show the event
if you have any questions for more info just ask, I'll be around
Try with this, create this function
func gotoAppleCalendar(date: NSDate) {
let interval = date.timeIntervalSinceReferenceDate
let url = NSURL(string: "calshow:\(interval)")!
UIApplication.sharedApplication().openURL(url)
}
Call the function using the event start date as parameter
gotoAppleCalendar(event.startDate)
This open the apple calendar showing the added event
Swift 4 variant+
func gotoAppleCalendar(date: Date) {
let interval = date.timeIntervalSinceReferenceDate
let url = URL(string: "calshow:\(interval)")!
UIApplication.shared.openURL(url)
}
Sure that's what I'm trying to clarify... you said you'll see an "added event"... as if you added the event with the code you wrote, but you did not do that.
Which, is confusing, when you're searching for how to add a calendar event on google and you get an answer that says "added event"
Swift 4 CalendarService which can create Event and Open Calendar.
import Foundation
import EventKit
import UIKit
final class CalendarService {
class func openCalendar(with date: Date) {
guard let url = URL(string: "calshow:\(date.timeIntervalSinceReferenceDate)") else {
return
}
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
class func addEventToCalendar(title: String,
description: String?,
startDate: Date,
endDate: Date,
completion: ((_ success: Bool, _ error: NSError?) -> Void)? = nil) {
DispatchQueue.global(qos: .background).async { () -> Void in
let eventStore = EKEventStore()
eventStore.requestAccess(to: .event, completion: { (granted, error) in
if (granted) && (error == nil) {
let event = EKEvent(eventStore: eventStore)
event.title = title
event.startDate = startDate
event.endDate = endDate
event.notes = description
event.calendar = eventStore.defaultCalendarForNewEvents
do {
try eventStore.save(event, span: .thisEvent)
} catch let e as NSError {
DispatchQueue.main.async {
completion?(false, e)
}
return
}
DispatchQueue.main.async {
completion?(true, nil)
}
} else {
DispatchQueue.main.async {
completion?(false, error as NSError?)
}
}
})
}
}
}
Using
CalendarService.addEventToCalendar(title: "TITLE",
description: "DESCRIPTION",
startDate: startDate,
endDate: endDate,
completion: { (success, error) in
if success {
CalendarService.openCalendar(with: startDate)
} else if let error = error {
print(error)
}
})