gpt4 book ai didi

ios - 将事件保存到用户的日历

转载 作者:IT王子 更新时间:2023-10-29 05:41:26 24 4
gpt4 key购买 nike

如何将事件添加到用户的日历,然后允许用户选择日历等。我有这段有效的代码,但这会将事件添加到用户的默认日历。我如何允许用户更改日历、自定义警报等?我已经看到其他应用程序打开日历应用程序并预​​填字段。

//add to calendar
let eventStore : EKEventStore = EKEventStore()
eventStore.requestAccessToEntityType(EKEntityType.Event, completion: { (granted, error) in
if granted && error == nil {
let event:EKEvent = EKEvent(eventStore: eventStore)

event.title = "My event: " + self.event.name
event.startDate = self.event.startTime
event.endDate = self.event.endTime
event.notes = self.event.description
event.calendar = eventStore.defaultCalendarForNewEvents

do {
try eventStore.saveEvent(event, span: .ThisEvent, commit: true)
self.dismissViewControllerAnimated(true, completion: {})
} catch {
self.dismissViewControllerAnimated(true, completion: {})
}
} else {
self.dismissViewControllerAnimated(true, completion: {})
}
})

最佳答案

您可以使用 Apple 的 native 日历 API。使用 EKEventEditViewControllerEventKitUI framework , 并且用户将能够在保存事件时指定日历。在 Swift 3 中:

import UIKit
import EventKit
import EventKitUI

class ViewController: UIViewController {

let store = EKEventStore()

func createEvent() {
// create the event object

let event = EKEvent(eventStore: store)
event.title = "Foo"
event.startDate = ...
event.endDate = ...

// prompt user to add event (to whatever calendar they want)

let controller = EKEventEditViewController()
controller.event = event
controller.eventStore = store
controller.editViewDelegate = self
present(controller, animated: true)
}
}

extension ViewController: EKEventEditViewDelegate {

func eventEditViewController(_ controller: EKEventEditViewController, didCompleteWith action: EKEventEditViewAction) {
controller.dismiss(animated: true)
}
}

在 Swift 2.3 中:

import UIKit
import EventKit
import EventKitUI

class ViewController: UIViewController {

let store = EKEventStore()

func createEvent() {
// create the event object

let event = EKEvent(eventStore: store)
event.title = "Foo"
event.startDate = ...
event.endDate = ...

// prompt user to add event (to whatever calendar they want)

let controller = EKEventEditViewController()
controller.event = event
controller.eventStore = store
controller.editViewDelegate = self
presentViewController(controller, animated: true, completion: nil)
}
}

extension ViewController: EKEventEditViewDelegate {

func eventEditViewController(controller: EKEventEditViewController, didCompleteWithAction action: EKEventEditViewAction) {
controller.dismissViewControllerAnimated(true, completion: nil)
}
}

这假设您已经在您的 Info.plist 中提供了一个 NSCalendarsUsageDescription,您已经 requested access

关于ios - 将事件保存到用户的日历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42754868/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com