1

我在日历中添加了一些事件并将它们的 eventIdentifier 保存到文件中。当我想删除我的所有事件时,我从该文件中读取 eventIdentifier 到一个数组并删除每个事件及其事件 ID。以下是将事件添加到日历并将其事件 ID 保存到文件的代码:

  - (void) addEventToCalendar: (id)object
    { 
        @autoreleasepool {    
            int i = 0;
            NSString *string_to_file = @"";
            eventStore=[[EKEventStore alloc] init];
            for(Schedule *sche in scheduleArray){ 

                EKEvent *addEvent=[EKEvent eventWithEventStore:eventStore];
                addEvent.title=sche.course_Name;
                addEvent.startDate = [self stringToDate:sche.from_Date];
                addEvent.endDate = [self stringToDate:sche.thru_Date];
                NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 
                [addEvent setCalendar:[eventStore defaultCalendarForNewEvents]];
                NSDate *date_alarm = [addEvent.startDate dateByAddingTimeInterval:-(10*60)];  
                addEvent.alarms=[NSArray arrayWithObject:[EKAlarm alarmWithAbsoluteDate:date_alarm]]; 
                NSError *err;
                // do save event to calendar
                [eventStore saveEvent:addEvent span:EKSpanThisEvent error:&err];
                if (err == nil) {
                    NSString* str = [[NSString alloc] initWithFormat:@"%@", addEvent.eventIdentifier]; 
                    string_to_file = [string_to_file stringByAppendingString:str];
                    string_to_file = [string_to_file stringByAppendingString:@"\n"];
                    NSLog(@"String %d: %@",i, str);
                }
                else {
                    NSLog(@"Error %@",err);
                }
                i++;
            } 
            // create file to save
            [[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil]; 
            inFile = [NSFileHandle fileHandleForWritingAtPath: filePath]; 
            NSData *data = [string_to_file dataUsingEncoding:NSUTF16StringEncoding]; 
            [inFile writeData:data];
        } 
    }

下面的代码删除我添加到日历中的所有事件

- (void) deleteEventInCalender { 
    filemgr = [NSFileManager defaultManager]; 
    NSString *filePath = [self getFilePath:@"saveeventid.txt"];
    NSFileHandle *inFile; 
    inFile = [NSFileHandle fileHandleForReadingAtPath:filePath];
    NSData *dataFile; 
    dataFile = [inFile readDataToEndOfFile];  
    NSString *tmp =  @"";
    NSString *temp =  @"";
    tmp = [NSString stringWithCharacters:[dataFile bytes] length:[dataFile length]/sizeof(unichar)];
    if(![tmp isEqualToString:@""]){ 
        tmp = [tmp substringFromIndex:1]; 
        event_idArray = [[NSMutableArray alloc] init];
        int j = 0;
        while (![tmp isEqualToString:@""]){ 
            int index_find_string = [tmp rangeOfString:@"\n"].location;
            temp = [tmp substringWithRange:NSMakeRange(0, index_find_string)];  
            [event_idArray addObject:temp]; 
            tmp = [tmp substringFromIndex:index_find_string + 1]; 
        }

        EKEventStore* store = [[EKEventStore alloc] init];
        j = 0;
        for(NSString *eventid in event_idArray){

            EKEvent* event2 = [store eventWithIdentifier:eventid];
            if (event2 != nil) {
                 NSLog(@"log: %d log id: %@", j, eventid);
                NSError* error = nil;
                // remove event
                [store removeEvent:event2 span:EKSpanThisEvent error:&error];
            }
            j++;
        } 
        [filemgr removeItemAtPath:filePath error:nil]; 
    }
}

当我使用 calendar.sqlitedb 在 iOS 模拟器上进行测试时,上面的所有代码都运行良好。但是当我在 iPad 设备 5.0 上运行时会出现一些奇怪的错误。那是有时日历没有删除事件,或者当所有事件都被删除时,几分钟后所有事件再次出现......我不明白,我不知道为什么,我很困惑。有没有人和我有同样的问题?请分享您的解决方案!

添加了另一个问题:日历数据库存储在 iOS 5.0 设备中的位置。

4

0 回答 0