2017-05-09 64 views
1

我創建iOS應用程序,它從服務器獲取日期並將它們添加活動的日曆刪除所有事件從日曆

- (void)addReminderWithTitle:(NSString *)title date:(NSString *)date { 
    NSString *dateString = date; 
    NSDateFormatter *dateFormatter = [NSDateFormatter new]; 
    [dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"]; 
    NSDate *dateFromString = [dateFormatter dateFromString:dateString]; 
    [dateFromString dateByAddingTimeInterval:-60*30];  
    EKEventStore *eventStore = [[EKEventStore alloc] init]; 
    [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) { 
     if (granted) { 
     EKEvent *event = [EKEvent eventWithEventStore:eventStore]; 
     event.title = title; 
     event.startDate = dateFromString; 
      //[NSDate date]; //today 
     event.endDate = [event.startDate dateByAddingTimeInterval:60*60]; //set 1 hour meeting 
     event.calendar = [eventStore defaultCalendarForNewEvents]; 
     NSError *err = nil; 
     [eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&err]; 
     }}]; 
     } 

,我需要一個功能,這將刪除用戶在特定操作中的所有事件。 有幫助嗎?

+0

可能重複:http://stackoverflow.com/questions/28321921/how-to-delete-all-the-事件 - 我的應用程序日曆 - 當應用程序被刪除從設備?rq = 1 –

回答

0

您需要從日曆中獲取所有事件的數組,並通過標識符,日期或標題將它們過濾到數組中,以便您可以獲取自己添加的事件。

// Create the predicate from the event store's instance method 
NSPredicate *predicate = [store predicateForEventsWithStartDate:oneDayAgo endDate:oneYearFromNow calendars:nil]; 

// Fetch all events that match the predicate 
NSArray *events = [store eventsMatchingPredicate:predicate]; 

隨後的events陣列上環和刪除它們這樣

EKEventStore* store = [[EKEventStore alloc] init]; 
    EKEvent* event2 = [store eventWithIdentifier:[arrayofCalIDs objectAtIndex:i]]; 
if (event2 != nil) { 
    NSError* error = nil; 
    [store removeEvent:event2 span:EKSpanThisEvent error:&error]; 
} 
+0

從哪裏得到「arrayofCalIDs」? –

+0

在這行之後'NSArray * events = [store eventsMatchingPredicate:predicate];'你會有'EKEvent'數組,如果你在'events'數組上運行一個循環,你可以得到每個事件。 PS:忘記'arrayofCalIDs',那是我的代碼 –

+0

即時通訊沒有得到你的觀點,你能寫出它嗎? –