加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何使用swift在设备日历中添加事件

发布时间:2020-12-14 05:51:31 所属栏目:百科 来源:网络整理
导读:我有兴趣知道如何在设备中添加日历事件,但使用swift。我知道在Objective-C中有一些例子,但目前还没有什么是迅速的。非常感谢。 Swift 3.0版本 let eventStore : EKEventStore = EKEventStore() // 'EKEntityTypeReminder' or 'EKEntityTypeEvent' eventSto
我有兴趣知道如何在设备中添加日历事件,但使用swift。我知道在Objective-C中有一些例子,但目前还没有什么是迅速的。非常感谢。
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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读