问题描述:

i'm trying to add a calendar to the iCloud EKSource, i'm trying to find the iCloud calendar in this way:

EKSource *theSource = nil;

for (EKSource *source in eventStore.sources) {

if (source.sourceType == EKSourceTypeCalDAV && [source.title isEqualToString:@"iCloud"]) {

theSource = source;

break;

}

}

but don't find my any EKSource, how i can add a calendar on icloud and see how many calendar are in icloud?

网友答案:

Perhaps something like this will help you..

- (void)saveEventToiCloud {

    EKEventStore *eventDB = [[EKEventStore alloc] init];
    NSArray *calender = [[eventDB calendars] retain];

    NSString * typeString = @"";
    EKCalendar *iCloud;
    for (EKCalendar *thisCalendar in calenders) 
    {
        EKCalendarType type = thisCalendar.type;
        if (type == EKCalendarTypeCalDAV)
        {
                NSString *calName=[NSString stringWithFormat:@"%@", thisCalendar.title];
                NSRange range = [calName rangeOfString:@"iCloud"];  //Couldn't find better way, if there is, then tell me too. :)
                if(range.length!=0)
                {
                        //icloud
                        iCloud = thisCalendar;
                        break;
                }
                else
                {
                        //any other email account     
                }
        }
    }
    if(iCloud){
        NSError *error = nil;
        EKEvent *thisEvent = [[EKEvent eventWithEventStore:eventDB] retain];
        thisEvent.title = @"Title";
        thisEvent.startDate = [NSDate date];
        thisEvent.endDate = [NSDate date]; 
        thisEvent.calendar = iCloud ;
        BOOL saved = [eventDB saveEvent:thisEvent span:EKSpanThisEvent error:&error];
        NSLog(@"Saved calendar event = %@\n", (saved ? @"YES" : @"NO"));
        [thisEvent release];
        thisEvent=nil;
    }
    [events release];
    [eventDB release];
}
相关阅读:
Top