【问题标题】:How to we add/remove certain calendars from my app我们如何从我的应用程序中添加/删除某些日历
【发布时间】:2016-11-01 11:31:10
【问题描述】:

我正在开发提醒应用程序,我可以同步屏幕上的所有日历事件 -

我的问题是可以添加/删除某些日历吗?这样我就可以只同步已添加的日历事件。

【问题讨论】:

    标签: ios calendar eventkit


    【解决方案1】:

    假设你使用的是swift,你可以像这样创建一个日历

            // Create an Event Store instance
                let eventStore = EKEventStore();
    
            // Use Event Store to create a new calendar instance
            // Configure its title
                let newCalendar = EKCalendar(forEntityType: .Event, eventStore: eventStore)
    
            // Probably want to prevent someone from saving a calendar
            // if they don't type in a name...
                        newCalendar.title = "Some Calendar Name"
    
            // Access list of available sources from the Event Store
                        let sourcesInEventStore = eventStore.sources
    
            // Filter the available sources and select the "Local" source to assign to the new calendar's
            // source property
                        newCalendar.source = sourcesInEventStore.filter{
                            (source: EKSource) -> Bool in
                source.sourceType.rawValue == EKSourceType.Local.rawValue
            }.first!
    
            // Save the calendar using the Event Store instance
                        do
                        {
                            try eventStore.saveCalendar(newCalendar, commit: true)
    
                NSUserDefaults.standardUserDefaults().setObject(newCalendar.calendarIdentifier, forKey: "EventTrackerPrimaryCalendar")
                        }
                            catch
                            {
                                let alert = UIAlertController(title: "Calendar could not save", message: (error as NSError).localizedDescription, preferredStyle: .Alert)
    
                let OKAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
    
                alert.addAction(OKAction)
    
    
                self.presentViewController(alert, animated: true, completion: nil)
                          }
                    }
                }
    

    将事件添加到特定日历

                    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 = "Test Title" 
    
                event.startDate = NSDate() 
    
                event.endDate = NSDate() 
    
                event.notes = "This is a note" 
    
                event.calendar = newCalendar
    
                eventStore.saveEvent(event, span: EKSpanThisEvent, error: nil) 
    
                println("Saved Event") 
            }
            }) 
    

    并且可以获取特定日历的事件

            func loadEvents() {
                // Create a date formatter instance to use for converting a string to a date
                let dateFormatter = NSDateFormatter()
    
        dateFormatter.dateFormat = "yyyy-MM-dd"
    
        // Create start and end date NSDate instances to build a predicate for which events to select
                let startDate = dateFormatter.dateFromString("2016-01-01")
    
        let endDate = dateFormatter.dateFromString("2016-12-31")
    
    
        if let startDate = startDate, endDate = endDate {
                    let eventStore = EKEventStore()
    
            // Use an event store instance to create and properly configure an NSPredicate
                    let eventsPredicate = eventStore.predicateForEventsWithStartDate(startDate, endDate: endDate, calendars: [newCalendar])
    
            // Use the configured NSPredicate to find and return events in the store that match
                    self.events = eventStore.eventsMatchingPredicate(eventsPredicate).sort(){
                        (e1: EKEvent, e2: EKEvent) -> Bool in
                return e1.startDate.compare(e2.startDate) == NSComparisonResult.OrderedAscending
    
            }
                }
            }
    

    【讨论】:

      猜你喜欢
      • 2011-09-02
      • 2015-04-04
      • 1970-01-01
      • 2016-11-20
      • 2020-12-17
      • 2013-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多