在iOS设备的日历中操作事件时会发生一些奇怪的错误

时间:2013-03-12 14:24:20

标签: iphone objective-c xcode ios5 eventkit

我在日历中添加了一些事件,并将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设备中的位置。

0 个答案:

没有答案