如何用事件打开日历 - NSURL calshow:

时间:2015-08-08 18:29:58

标签: swift nsdate nsurl eventkit

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

我已经完成了一些研究,并且我已经提出了两种使用NSURL从应用内部打开原生日历的方法

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

    我还发现this website列出了calshow:x?eventid=id作为网址,但我不确定这是否有效(列为非公开)并且我无法让自己工作,尝试过使用:

    event.calendarItemExternalIdentifier
    event.eventIdentifier
    event.calendarItemIdentifier
    

    目前我正在使用此代码在finalInterval日期,事件日期打开日历应用

            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的日历或类似事件的日历

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

4 个答案:

答案 0 :(得分:9)

试试这个,创建这个功能

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

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

调用该函数
 gotoAppleCalendar(event.startDate)

这打开了显示添加事件的苹果日历

答案 1 :(得分:1)

Swift 4变种+

func gotoAppleCalendar(date: Date) {
     let interval = date.timeIntervalSinceReferenceDate
     let url = URL(string: "calshow:\(interval)")!
     UIApplication.shared.openURL(url)
 }

答案 2 :(得分:0)

当然这就是我要澄清的内容......你说你会看到一个“添加的事件”......好像你用你编写的代码添加了事件,但是你没有这样做。

当您正在搜索如何在Google上添加日历事件并获得“添加事件”的答案时,这很令人困惑

答案 3 :(得分:0)

Swift 4 CalendarService,可以创建事件和打开日历。

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