【问题标题】:How to open calendar with event - NSURL calshow:如何打开带有事件的日历 - NSURL calshow:
【发布时间】:2015-11-01 01:08:43
【问题描述】:

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

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

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

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

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

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

【问题讨论】:

    标签: swift nsdate nsurl eventkit


    【解决方案1】:

    试试这个,创建这个函数

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

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

     gotoAppleCalendar(event.startDate)
    

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

    【讨论】:

    • @duhseekoh 此功能不是用于向日历添加事件,而是用于打开和先前添加的事件
    【解决方案2】:

    Swift 4 变体+

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

    【讨论】:

      【解决方案3】:

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

      当您在 Google 上搜索如何添加日历活动时,您会得到一个“已添加活动”的答案,这很令人困惑

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 2018-03-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-06-17
          • 1970-01-01
          • 2023-03-19
          • 1970-01-01
          相关资源
          最近更新 更多