How to open calendar with event - NSURL calshow:

无人久伴 提交于 2019-12-04 18:01:23

问题


I'm wondering if there is someone who knows how to launch calendar with a specific event from APP

I've done some research and I've come up with two ways to open native calendar from inside the app using NSURL

  1. "calshow://" which opens calendar at current date
  2. "calshow:\(someNSDate.timeIntervalSinceReferenceDate)" which opens calendar with date of someNSDate

I also found this website that lists calshow:x?eventid=id as url but I'm not sure if this works (listed as not public) and I couldn't get it working myself, tried using :

event.calendarItemExternalIdentifier
event.eventIdentifier
event.calendarItemIdentifier

currently I'm using this code to open the calendar app at the finalInterval date, date of the event

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

What I'd like to do is to open up the calendar with an event id or something like that that would show the event

if you have any questions for more info just ask, I'll be around


回答1:


Try with this, create this function

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

Call the function using the event start date as parameter

 gotoAppleCalendar(event.startDate)

This open the apple calendar showing the added event




回答2:


Swift 4 variant+

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



回答3:


Sure that's what I'm trying to clarify... you said you'll see an "added event"... as if you added the event with the code you wrote, but you did not do that.

Which, is confusing, when you're searching for how to add a calendar event on google and you get an answer that says "added event"




回答4:


Swift 4 CalendarService which can create Event and Open Calendar.

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

Using

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


来源:https://stackoverflow.com/questions/31897199/how-to-open-calendar-with-event-nsurl-calshow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!