如何使用Swift在设备日历中添加事件 [英] How to add an event in the device calendar using swift

查看:184
本文介绍了如何使用Swift在设备日历中添加事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在设备中添加日历事件,但是要使用swift.我知道在Objective-C中有一些例子,但是目前还没有很快.非常感谢.

I would be interested in knowing how to add a calendar event in the device, but using swift. I know there are some examples made in Objective-C, but at the moment nothing in swift. Many thanks.

推荐答案

Swift 3.0版本

    let eventStore : EKEventStore = EKEventStore()

    // 'EKEntityTypeReminder' or 'EKEntityTypeEvent'

    eventStore.requestAccess(to: .event) { (granted, error) in

        if (granted) && (error == nil) {
            print("granted \(granted)")
            print("error \(error)")

            let event:EKEvent = EKEvent(eventStore: eventStore)

            event.title = "Test Title"
            event.startDate = Date()
            event.endDate = Date()
            event.notes = "This is a note"
            event.calendar = eventStore.defaultCalendarForNewEvents
            do {
                try eventStore.save(event, span: .thisEvent)
            } catch let error as NSError {
                print("failed to save event with error : \(error)")
            }
            print("Saved Event")
        }
        else{

            print("failed to save event with error : \(error) or access not granted")
        }
    }   

参考:https://gist.github.com/mchirico/d072c4e38bda61040f91

Reference : https://gist.github.com/mchirico/d072c4e38bda61040f91

这篇关于如何使用Swift在设备日历中添加事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆