将活动保存到用户的日历

时间:2017-03-13 00:24:09

标签: ios swift swift2 ekevent ekeventstore

如何将事件添加到用户的日历中,然后允许用户选择日历等。我有此代码可用,但这会将事件添加到用户的默认设置日历。如何允许用户更改日历,自定义警报等?我见过其他应用程序打开日历应用程序并预​​先填写字段。

int bold_italic = Text.BOLD | Text.ITALIC;
text.flags = (text.flags & ~bold_italic) | bold_italic;

3 个答案:

答案 0 :(得分:14)

您可以使用Apple的原生日历API。在EKEventEditViewController中使用EventKitUI 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)
    }
}

这假设您在NSCalendarsUsageDescription中提供了Info.plistrequested access等等。

答案 1 :(得分:0)

Apple没有原生日历API。

试试这个第三方库Calendar。它看起来与iOS calandar应用程序完全相同,并且它也与EKEventStore集成。我在我的项目中使用过这个库没有任何问题。

答案 2 :(得分:0)

在Swift 4.2中完美运行

import UIKit
import EventKit
import EventKitUI

class yourViewController: UIViewController{

    let eventStore = EKEventStore()

    func addEventToCalendar() {

    eventStore.requestAccess( to: EKEntityType.event, completion:{(granted, error) in
        DispatchQueue.main.async {
            if (granted) && (error == nil) {
                let event = EKEvent(eventStore: self.eventStore)
                event.title = self.headerDescription
                event.startDate = self.parse(self.requestDetails.value(forKey: "session_time") as? String ?? "")
                event.endDate = self.parse(self.requestDetails.value(forKey: "session_end_time") as? String ?? "")
                let eventController = EKEventEditViewController()
                eventController.event = event
                eventController.eventStore = self.eventStore
                eventController.editViewDelegate = self
                self.present(eventController, animated: true, completion: nil)

            }
        }


       })
    }

}

现在将出现以下屏幕,您还可以在此处根据要求自定义详细信息:

enter image description here

//现在添加事件后关闭视图控制器

extension yourViewController: EKEventEditViewDelegate {

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

    }
}

注意:不要忘记将 NSCalendarsUsageDescription 键添加到信息列表中。