Swift-use Eventkit to get System calendar events, add events

Source: Internet
Author: User

iOS calendar events can be read, added, and so on via Eventkit. But what we found on the Internet was written using OBJECTIVE-C.

Here is a swift version of the sample that shows you how to add an event and get all the list of events.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 import UIKitimport EventKitclass ViewController: UIViewController {    override func viewDidLoad() {        super.viewDidLoad()        // Do any additional setup after loading the view, typically from a nib.                var eventStore:EKEventStore = EKEventStore()                // ‘EKEntityTypeReminder‘ or ‘EKEntityTypeEvent‘        eventStore.requestAccessToEntityType(EKEntityTypeEvent, completion: {            granted, error in            if (granted) && (error == nil) {                println("granted \(granted)")                println("error  \(error)")                                // 新建一个事件                var event:EKEvent = EKEvent(eventStore: eventStore)                event.title = "新增一个测试事件"                event.startDate = NSDate()                event.endDate = NSDate()                event.notes = "这个是备注"                event.calendar = eventStore.defaultCalendarForNewEvents                eventStore.saveEvent(event, span: EKSpanThisEvent, error: nil)                println("Saved Event")                                // 获取所有的事件(前后90天)                var startDate=NSDate().dateByAddingTimeInterval(-3600*24*90)                var endDate=NSDate().dateByAddingTimeInterval(3600*24*90)                var predicate2 = eventStore.predicateForEventsWithStartDate(startDate,                    endDate: endDate, calendars: nil)                                println("查询范围 开始:\(startDate) 结束:\(endDate)")                var eV = eventStore.eventsMatchingPredicate(predicate2) as [EKEvent]!                                if eV != nil {                    for i in eV {                        println("标题  \(i.title)" )                        println("开始时间: \(i.startDate)" )                        println("结束时间: \(i.endDate)" )                    }                }            }        })    }    override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()        // Dispose of any resources that can be recreated.    }}

Swift-use Eventkit to get System calendar events, add events

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.