swift- 如何在特定日期和时间打开日历应用程序?
swift- how to open up calendar app at specific date and time?
我找不到准确的答案,但我只想:
- 用户单击按钮并传入以毫秒为单位的日期
- 我的应用会在特定日期和时间打开 IOS 默认日历应用。
我正在使用此代码:
let date = Date(timeIntervalSince1970: TimeInterval(timeInMiliFromBackEnd/1000))
let interval = date.timeIntervalSince1970
let url = NSURL(string: "calshow:\(interval)")!
UIApplication.shared.openURL(url as URL)
日历应用打开,但它在某个随机年份(2066 年 1 月 @ 下午 3 点)打开。
任何人都可以为此提供 swift 3 代码吗?
干杯~
iOS 不使用 timeIntervalSince1970
,因为它是 "epoch date." 您应该使用 timeIntervalSinceReferenceDate
:
//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)
}
(上面的代码已更新为使用更新的 open(_:options:completionHandler:)
方法,而不是现在已弃用的 openURL(_:)
方法。)
我找不到准确的答案,但我只想:
- 用户单击按钮并传入以毫秒为单位的日期
- 我的应用会在特定日期和时间打开 IOS 默认日历应用。
我正在使用此代码:
let date = Date(timeIntervalSince1970: TimeInterval(timeInMiliFromBackEnd/1000))
let interval = date.timeIntervalSince1970
let url = NSURL(string: "calshow:\(interval)")!
UIApplication.shared.openURL(url as URL)
日历应用打开,但它在某个随机年份(2066 年 1 月 @ 下午 3 点)打开。
任何人都可以为此提供 swift 3 代码吗?
干杯~
iOS 不使用 timeIntervalSince1970
,因为它是 "epoch date." 您应该使用 timeIntervalSinceReferenceDate
:
//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)
}
(上面的代码已更新为使用更新的 open(_:options:completionHandler:)
方法,而不是现在已弃用的 openURL(_:)
方法。)