Swift 如何使用事件打开日历-NSURL calshow:
我想知道是否有人知道如何从应用程序启动带有特定事件的日历 我做了一些研究,提出了两种使用NSURL从应用程序内部打开本机日历的方法Swift 如何使用事件打开日历-NSURL calshow:,swift,nsdate,nsurl,eventkit,Swift,Nsdate,Nsurl,Eventkit,我想知道是否有人知道如何从应用程序启动带有特定事件的日历 我做了一些研究,提出了两种使用NSURL从应用程序内部打开本机日历的方法 “calshow://”在当前日期打开日历 “calshow:\(someNSDate.timeIntervalSinceReferenceDate)”打开日期为someNSDate的日历 我还发现将calshow:x?eventid=id列为url,但我不确定这是否有效(列为非公开),我自己也无法让它工作,尝试使用: event.calendarItemExter
“calshow://”
在当前日期打开日历“calshow:\(someNSDate.timeIntervalSinceReferenceDate)”
打开日期为someNSDate的日历
calshow:x?eventid=id
列为url,但我不确定这是否有效(列为非公开),我自己也无法让它工作,尝试使用:
event.calendarItemExternalIdentifier
event.eventIdentifier
event.calendarItemIdentifier
目前,我正在使用此代码在最后一个日历日期(活动日期)打开日历应用程序
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)")!)
}
我想做的是打开日历,用事件id或类似的东西来显示事件
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)")!)
}
如果您有任何关于更多信息的问题,请提问,我将在附近尝试使用此功能,创建此功能
func gotoAppleCalendar(date: NSDate) {
let interval = date.timeIntervalSinceReferenceDate
let url = NSURL(string: "calshow:\(interval)")!
UIApplication.sharedApplication().openURL(url)
}
使用事件开始日期作为参数调用函数
gotoAppleCalendar(event.startDate)
这将打开显示添加事件的apple日历确定这就是我想要澄清的。。。你说过你会看到一个“附加事件”。。。好像您用编写的代码添加了事件,但您没有这样做
当你在谷歌上搜索如何添加日历事件时,你得到的答案是“添加事件”Swift 4 variant+
func gotoAppleCalendar(date: Date) {
let interval = date.timeIntervalSinceReferenceDate
let url = URL(string: "calshow:\(interval)")!
UIApplication.shared.openURL(url)
}
Swift 4日历服务,可创建活动并打开日历
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?)
}
}
})
}
}
}
使用
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)
}
})
@duhseekoh此函数不用于将事件添加到日历,而是用于打开和先前添加的事件