如何更新和删除日历活动?

时间:2012-09-15 06:58:32

标签: iphone events calendar

我正在使用iPhone日历事件做一个示例应用程序。我可以将日历事件从我的应用程序保存到iPhone本机日历。保存时我将保存的事件标识符保存到我的应用程序数据库中。现在我需要更新同一事件或删除事件。如何使用保存在我的数据库中的事件标识符。以下是用于保存事件并将事件标识符保存到数据库中的代码。

ENTITYCMAPPIPHONE *appointmentCM;  
NSError *error;  

NSFetchRequest *fetchRequest = 
[managedObjectModel fetchRequestFromTemplateWithName:@"FetchForIphoneAppointment" substitutionVariables:nil];  

NSArray *fetchedObjects= [context executeFetchRequest:fetchRequest error:nil];

int fetchCount = [fetchedObjects count];   
if ([fetchedObjects count]>0) {   
    for (appointmentCM in fetchedObjects) {       
        NSLog(@"Appoint subject %@", appointmentCM.subject);
        NSLog(@"APP notes %@", appointmentCM.notes);

            appoint_ID = [appointmentCM.mobAppID intValue]; 

            NSDateFormatter *f = [[NSDateFormatter alloc] init];
            [f setDateFormat:@"yyyy-MM-dd HH:mm:ss"];  
            NSDate *date = [f dateFromString:@"2012-09-10 13:55:15"]; 

            NSCalendar *calendar = [NSCalendar currentCalendar];
            NSInteger units = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit;
            NSDateComponents *components = [calendar components:units fromDate:date];
            NSInteger year = [components year];
            NSInteger day = [components day];

            NSDateFormatter *weekDay = [[[NSDateFormatter alloc] init] autorelease];
            [weekDay setDateFormat:@"EEE"];

            NSDateFormatter *calMonth = [[[NSDateFormatter alloc] init] autorelease];
            [calMonth setDateFormat:@"MMMM"];


            EKEventStore *eventStore1 = [[EKEventStore alloc] init];

            EKEvent *event  = [EKEvent eventWithEventStore:eventStore1];  
            event.title = appointmentCM.subject;                 
            event.notes = appointmentCM.notes;                   
            event.startDate = date;
            event.endDate   = [[NSDate alloc] initWithTimeInterval:600         sinceDate:event.startDate];

            [event setCalendar:[eventStore1 defaultCalendarForNewEvents]];
            NSError *err;
            [eventStore1 saveEvent:event span:EKSpanThisEvent error:&err];       

            eventID = [[NSString alloc] initWithFormat:@"%@", event.eventIdentifier];  
            [self updateIphoneCalendarEventID];
        }


        if (![context save:&error]) {
            NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);

        }            
    }    
}       


-(void)updateIphoneCalendarEventID{  
NSError *error;  

NSDictionary *subs; 
subs = [NSDictionary
        dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:appoint_ID], @"search_appID", nil];
NSFetchRequest *fetchRequestCheckNil = 
[managedObjectModel fetchRequestFromTemplateWithName:
 @"FetchEditIphoneCalendar" substitutionVariables:subs];

NSArray *fetchedObjects=[context executeFetchRequest:fetchRequestCheckNil error:nil];
if ([fetchedObjects count]>0) {
    for (ENTITYAPPOINTMENTS *appDetails
         in fetchedObjects) {                                    
        [self updateDBModelForAppointments:appDetails];              
    }     
}     
if (![context save:&error]) {
    NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
} 

}       

-(void)updateDBModelForAppointments:(ENTITYAPPOINTMENTS *)contactDetails{ 
contactDetails.iphoneAppID = eventID;   
}    

1 个答案:

答案 0 :(得分:2)

    EKEventStore *eventStore = [[[EKEventStore alloc] init] autorelease];
    NSString *idenfier = nil;//.....;// Get identifier stored in your application database
    NSError *err = nil;
    EKEvent *ev = [eventStore eventWithIdentifier:idenfier];

        if(err){
            NSLog(@"Error: %@",[err localizedDescription]);
        }
        else
        {

            // manipulate event...
            ev.title     = @"new title";
            ev.location = @"new location";

            // or remove:
           [eventStore removeEvent:ev span:EKSpanFutureEvents commit:NO error:&err];
        }