EKCalendar SaveEKCalendar(EKSource source, string calendarName, string color = null) { var calendar = EKCalendar.Create(EKEntityType.Event, _eventStore); //Setup calendar to be inserted calendar.Title = calendarName; NSError error = null; if (!string.IsNullOrEmpty(color)) { calendar.CGColor = ColorConversion.ToCGColor(color); } calendar.Source = source; if (_eventStore.SaveCalendar(calendar, true, out error)) { return(calendar); } _eventStore.Reset(); return(null); }
/// <summary> /// Tries to create a new calendar with the specified source/name/color. /// May fail depending on source. /// </summary> /// <remarks> /// This is only intended as a helper method for CreateEKCalendar, /// not to be called independently. /// </remarks> /// <returns>The created native calendar, or null on failure</returns> /// <param name="source">Calendar source (e.g. iCloud vs local vs gmail)</param> /// <param name="calendarName">Calendar name.</param> /// <param name="color">Calendar color.</param> private EKCalendar SaveEKCalendar(EKSource source, string calendarName, string color = null) { var calendar = EKCalendar.Create(EKEntityType.Event, _eventStore); // Setup calendar to be inserted // calendar.Title = calendarName; if (!string.IsNullOrEmpty(color)) { calendar.CGColor = ColorConversion.ToCGColor(color); } calendar.Source = source; NSError error = null; if (_eventStore.SaveCalendar(calendar, true, out error)) { Console.WriteLine($"Successfully saved calendar with source {source.Title}"); // TODO: Should we try calling GetCalendars to make sure that // the calendar isn't hidden?? // return(calendar); } else { Console.WriteLine($"Tried and failed to save calendar with source {source.Title}"); } _eventStore.Reset(); return(null); }
EKCalendar SaveEKCalendar(EKSource source, string calendarName, string color = null) { var calendar = EKCalendar.Create(EKEntityType.Event, _eventStore); //Setup calendar to be inserted calendar.Title = calendarName; NSError error = null; if (!string.IsNullOrEmpty(color)) { calendar.CGColor = ColorConversion.ToCGColor(color); } calendar.Source = source; if (_eventStore.SaveCalendar(calendar, true, out error)) { return calendar; } _eventStore.Reset(); return null; }
private void AddGoHejaCalendarToDevice() { try { NSError error; ////remove existing descending events from now in "goHeja Events" calendar of device. var calendars = App.Current.EventStore.GetCalendars(EKEntityType.Event); foreach (var calendar in calendars) { if (calendar.Title == PortableLibrary.Constants.DEVICE_CALENDAR_TITLE) { goHejaCalendar = calendar; EKCalendar[] calendarArray = new EKCalendar[1]; calendarArray[0] = calendar; NSPredicate pEvents = App.Current.EventStore.PredicateForEvents(NSDate.Now.AddSeconds(-(3600 * 10000)), NSDate.Now.AddSeconds(3600 * 10000), calendarArray); EKEvent[] allEvents = App.Current.EventStore.EventsMatching(pEvents); if (allEvents == null) { continue; } foreach (var pEvent in allEvents) { NSError pE; DateTime now = DateTime.Now; DateTime startNow = new DateTime(now.Year, now.Month, now.Day); var startString = baseVC.ConvertDateTimeToNSDate(startNow); if (pEvent.StartDate.Compare(startString) == NSComparisonResult.Descending) { App.Current.EventStore.RemoveEvent(pEvent, EKSpan.ThisEvent, true, out pE); } } } } if (goHejaCalendar == null) { goHejaCalendar = EKCalendar.Create(EKEntityType.Event, App.Current.EventStore); EKSource goHejaSource = null; foreach (EKSource source in App.Current.EventStore.Sources) { if (source.SourceType == EKSourceType.CalDav && source.Title == "iCloud") { goHejaSource = source; break; } } if (goHejaSource == null) { foreach (EKSource source in App.Current.EventStore.Sources) { if (source.SourceType == EKSourceType.Local) { goHejaSource = source; break; } } } if (goHejaSource == null) { return; } goHejaCalendar.Title = PortableLibrary.Constants.DEVICE_CALENDAR_TITLE; goHejaCalendar.Source = goHejaSource; } App.Current.EventStore.SaveCalendar(goHejaCalendar, true, out error); if (error == null) { AddEvents(); } } catch (Exception e) { new UIAlertView("add events process", e.Message, null, "ok", null).Show(); } }