/// <summary> /// Removes an event from the specified calendar. /// </summary> /// <param name="calendar">Calendar to remove event from</param> /// <param name="calendarEvent">Event to remove</param> /// <returns>True if successfully removed</returns> /// <exception cref="System.ArgumentException">Calendar is read-only</exception> /// <exception cref="System.UnauthorizedAccessException">Calendar access denied</exception> /// <exception cref="System.InvalidOperationException">Editing recurring events is not supported</exception> /// <exception cref="Plugin.Calendars.Abstractions.PlatformException">Unexpected platform-specific error</exception> public async Task <bool> DeleteEventAsync(Calendar calendar, CalendarEvent calendarEvent) { if (string.IsNullOrEmpty(calendar.ExternalID) || string.IsNullOrEmpty(calendarEvent.ExternalID)) { return(false); } await RequestCalendarAccess().ConfigureAwait(false); var deviceCalendar = _eventStore.GetCalendar(calendar.ExternalID); if (deviceCalendar == null) { return(false); } var iosEvent = _eventStore.EventFromIdentifier(calendarEvent.ExternalID); if (iosEvent == null || iosEvent.Calendar.CalendarIdentifier != deviceCalendar.CalendarIdentifier) { return(false); } if (iosEvent.HasRecurrenceRules) { throw new InvalidOperationException("Editing recurring events is not supported"); } NSError error = null; if (!_eventStore.RemoveEvent(iosEvent, EKSpan.ThisEvent, true, out error)) { // Without this, the eventStore may act like the remove succeeded. // (this obviously also resets any other changes, but since we own the eventStore // we can be pretty confident that won't be an issue) // _eventStore.Reset(); if (error.Domain == _ekErrorDomain && error.Code == (int)EKErrorCode.CalendarReadOnly) { throw new ArgumentException(error.LocalizedDescription, nameof(calendar), new NSErrorException(error)); } else { throw new PlatformException(error.LocalizedDescription, new NSErrorException(error)); } } return(true); }
/// <summary> /// Removes all events. /// </summary> public static void RemoveAllEvents() { object[] calenderArray = new object[1]; calenderArray[0] = calendar; NSPredicate predicate = eventStore.PredicateForEvents(DateTime.Today, DateTime.Today.AddYears(1000), calenderArray); object[] eventObjs = eventStore.EventsMatchingPredicate(predicate); EKEvent eachEvent; foreach (object obj in eventObjs) { eachEvent = obj as EKEvent; eventStore.RemoveEvent(eachEvent, EKSpan.ThisEvent, null); } eventStore.SaveCalendar(calendar, true, null); }
public async Task <bool> DeleteAlarmAsync(string id) { bool hasPermission = await ASkForPermissionsAsync(); if (!hasPermission || appCalendar == null) { return(false); } var calendarEvent = eventsStore.GetCalendarItem(id); if (calendarEvent != null) { eventsStore.RemoveEvent((EKEvent)calendarEvent, EKSpan.ThisEvent, true, out NSError e); if (e == null) { return(true); } } return(false); }