1

我正在使用 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;   
}    
4

1 回答 1

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];
        }
于 2012-09-15T10:25:34.593 回答