2010-10-13 44 views

回答

12

这是绝对有可能创建自己的日历 - 渔获是,你需要的iOS 5:

EKEventStore* eventStore = [[EKEventStore alloc] init]; 
NSString* calendarName = @"My Cal"; 
EKCalendar* calendar; 

// Get the calendar source 
EKSource* localSource; 
for (EKSource* source in eventStore.sources) { 
    if (source.sourceType == EKSourceTypeLocal) 
    { 
     localSource = source; 
     break; 
    } 
} 

if (!localSource) 
    return; 

calendar = [EKCalendar calendarWithEventStore:eventStore]; 
calendar.source = localSource; 
calendar.title = calendarName; 

NSError* error; 
bool success= [eventStore saveCalendar:calendar commit:YES error:&error]; 
if (error != nil) 
{ 
    NSLog(error.description); 
    // TODO: error handling here 
} 
+0

你会如何检查日历是否已经存在?我不想一次又一次地意外创建日历? – Slee 2011-11-24 13:35:21

+0

当您创建日历时,有一个名为calendarIdentifier的属性是唯一标识符。存储该唯一标识符(以及事件存储标识符 - 确保没有更改),并在创建它之前检查事件存储以查看它是否存在。 – kurtzmarc 2011-11-26 04:11:12

+0

但这里有一个问题:我发现如果我的应用程序创建了本地日历,用户将无法在以后删除它。这看起来像一个错误。 – matt 2012-02-03 20:21:50

2

你(或其他人)在添加新日历方面有什么进展吗?

我有同样的情况。我可以通过编程方式将事件完美地添加到默认日历中,但我想将它们添加到新日历中,这样它们不会干扰用户存在的事件,并且可以很容易地被用户删除/隐藏,而不是删除所有事件手动。

您无法设置新的EKCalendar对象的属性。它看起来像只能将一个像defaultCalendarForNewEvents这样的现有类型分配给EKCalendar对象。

但是,我知道有可能以编程方式创建新日历,因为我已经看到iPhone应用程序正在执行此操作(无需离开应用程序)。

  • 难道是因为他们使用外部ICS文件做一些技巧的解决方法吗?
  • 也许可以通过“订阅”本地(在iPhone /应用程序文件系统中)生成的ICS文件而不是URL来完成此操作。有人对这个有经验么?
+0

这可能实际上工作!我会试试这个... – Marsson 2011-05-09 17:14:39

+0

关于这件事情有什么进展? – Buju 2011-09-08 09:01:22

0

这是你可以检查出是否日历已经与特定标题的存在。 如果它不存在,那么你可以编程创建它。

声明一个布尔类型变量

BOOL doesExist=NO; 
    EKEventStore *eventStore=[[EKEventStore alloc] init]; 

    NSArray *calanders=[eventStore calendarsForEntityType:EKEntityTypeEvent]; 

    //Now Iterate through every calendar in the array and match its title 
    // with the title that you want to create 



for(EKCalendar calendar in calendars) 
    { 
     if([[calendar title] isEqualToString:@"youdesiredname"]) 
      { 
        doesExist=YES; 
      } 

    } 

//所以现在检查,如果我们的布尔变量包含值YES这意味着具有相同名称/标题日历已经exists.if没有,那么你可以创建

if(!doesExist) 
    { 
     NSString* calendarName = @"DesiredCalendarName"; 
     EKCalendar* calendar; 


     EKSource* localSource; 
     for (EKSource* source in eventStore.sources) { 
     if (source.sourceType == EKSourceTypeLocal) 
     { 
     localSource = source; 
     break; 
     } 




    if (!localSource) 
     return; 

     calendar = [EKCalendar calendarWithEventStore:eventStore]; 
     calendar.source = localSource; 
     calendar.title = calendarName; 


     EKEvent *event = [EKEvent eventWithEventStore:eventStore]; 
     calendar = [eventStore calendarWithIdentifier:self.calendarIdentifier]; 
     event.calendar = calendar; 

     // Set the start date to the current date/time and the event duration to one hour 
     NSDate *startDate = [NSDate date]; 
     event.startDate = startDate; 
     event.endDate = [startDate dateByAddingTimeInterval:3600]; 

     //And to save the event to the event database: 

     NSError *error = nil; 
    BOOL result = [eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&error]; 
    if (result) 
    { 
    NSLog(@"Saved event to event store.") 
    } 
    else 
    { 
    NSLog(@"Error saving event: %@.", saveError); 
    } 

     NSError* error; 
     bool success= [eventStore saveCalendar:calendar commit:YES error:&error]; 
     if (error != nil) 
     { 
     NSLog(error.description); 

     } 

    }