TheQ
TheQ

Reputation: 2019

swift- how to open up calendar app at specific date and time?

I can't find an accurate answer, but all I want to do is:

  1. user clicks a button and passes in a date in miliseconds
  2. my app opens the IOS default calendar app at that specific date and time.

I was using this code:

let date = Date(timeIntervalSince1970: TimeInterval(timeInMiliFromBackEnd/1000))
let interval = date.timeIntervalSince1970
let url = NSURL(string: "calshow:\(interval)")!
        UIApplication.shared.openURL(url as URL)

The calendar app opens, however it opens in some random year (January 2066 @ 3pm).

Can anyone provide the swift 3 code for this?

Cheers~

Upvotes: 2

Views: 2047

Answers (1)

Duncan C
Duncan C

Reputation: 131491

iOS Doesn't use timeIntervalSince1970 as it's "epoch date." You should use timeIntervalSinceReferenceDate instead:

//Convert the time from the server into a Date object
let seconds = TimeInterval(timeInMiliFromBackEnd/1000)
let date = Date(timeIntervalSince1970: seconds)

//Convert the Date object into the number of seconds since the iOS "epoch date"
let interval = date.timeIntervalSinceReferenceDate
if let url = URL(string: "calshow:\(interval)") {
  UIApplication.shared.open(url, options: [:], completionHandler: nil)
}

(Code above updated to use newer open(_:options:completionHandler:) method, as opposed to the now-deprecated openURL(_:) method.)

Upvotes: 3

Related Questions