ios - 在 EKEvent 中为 iCal 设置特定的事件日期

标签 ios icalendar ekevent

我是 Xcode 的初学者,搜索了以下代码以将事件添加到我的应用程序中的 ical 中。但该示例仅向我展示了如何使用 [[NSDate alloc] init];

设置开始日期和结束日期

我可以获得什么代码来设置我想要的日期,例如20-12-2012?

感谢您的宝贵时间。

EKEventStore *eventDB = [[EKEventStore alloc] init];

EKEvent *myEvent  = [EKEvent eventWithEventStore:eventDB];

myEvent.title     = @"New Event";
myEvent.startDate = [[NSDate alloc]init ];

myEvent.endDate   = [[NSDate alloc]init ];
myEvent.allDay = YES;

[myEvent setCalendar:[eventDB defaultCalendarForNewEvents]];

NSError *err;

[eventDB saveEvent:myEvent span:EKSpanThisEvent error:&err]; 


    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"Event Created"
                          message:@"Yay!?"
                          delegate:nil
                          cancelButtonTitle:@"Okay"
                          otherButtonTitles:nil];
    [alert show];

最佳答案

正如您所发现的,将 NSDate 对象设置为重要日期是完全重要的。

有几种方法可以做到这一点。

1)

查看 the documentation for [NSDateFormatter dateFromString:] .

2)

这是另一种设置 2012 年 12 月 20 日的方法。

NSCalendar * gregorian = [NSCalendar currentCalendar];
NSDate *currentDate = [NSDate dateWithTimeIntervalSinceReferenceDate: 0];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear: 11]; // 11 years past the reference date of 1-January-2001
[comps setMonth: 11]; // 11 months past January
[comps setDay:19]; // 19 days past the first day of January
NSDate *eventDate = [gregorian dateByAddingComponents:comps toDate:currentDate  options:0];

NSLog( @"event date is %@", [eventDate description] );
[comps release]; // NSDateComponents was explicitly alloc'd & init'd

myEvent.startDate = eventDate;
mtEvent.endDate = eventDate;

关于ios - 在 EKEvent 中为 iCal 设置特定的事件日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8034330/

相关文章:

ios - 无需夏令时调整即可快速创建 EKEvent

google-calendar-api - 导入 .ics 文件时出现 Google 日历错误

php - 通过 zend_mail 发送 ical

ios - 如果可用,请在 Facebook 应用程序中打开链接

ios - CGRect 的表达式不可分配错误

javascript - 在javascript中为两个不同的时间获取相同的unix时间戳

ekevent - 如何获得ekevent EKparticipant电子邮件?

iphone - 此代码是否可以用于自定义 EKEventViews 的颜色(附有代码)

ios - iPad 中 UIModalPresentationFormSheet 的确切大小是多少

ios - 移动网站无法在iframe中呈现移动设备