如何打开带有事件的日历 - NSURL calshow:

How to open calendar with event - NSURL calshow:

我想知道是否有人知道如何从 APP 启动带有特定事件的日历

我做了一些研究,我想出了两种使用 NSURL 从应用程序内部打开本机日历的方法

  1. "calshow://" 在当前日期打开日历
  2. "calshow:\(someNSDate.timeIntervalSinceReferenceDate)" 打开日期为 someNSDate
  3. 的日历

我还发现 this websitecalshow:x?eventid=id 列为 url,但我不确定这是否有效(列为非 public)而且我不能让它自己工作,尝试使用:

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 或类似的东西打开日历,以显示事件

如果您对更多信息有任何疑问,请尽管提问,我会在附近

试试这个,创建这个函数

func gotoAppleCalendar(date: NSDate) {
  let interval = date.timeIntervalSinceReferenceDate
  let url = NSURL(string: "calshow:\(interval)")!
  UIApplication.sharedApplication().openURL(url)
}

使用事件开始日期作为参数调用函数

 gotoAppleCalendar(event.startDate)

这将打开显示添加事件的 Apple 日历

当然这就是我要澄清的...你说你会看到一个 "added event"...就好像你用你写的代码添加了事件,但你没有那样做.

令人困惑的是,当您搜索如何在 google 上添加日历事件时,您得到的答案是 "added event"

Swift 4 个变体+

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)
                                                }
    })