iCal事件 - 通过iOS应用程序以编程方式添加

时间:2012-07-23 05:16:04

标签: ios xcode icalendar

我有一个用户点击UIButton的场景,该事件会自动添加到我的iCal中。在导入#import框架并添加EKEventEditViewDelegate之后,我应该如何添加事件开始日期和标题等。所有这些信息都作为文本文件从服务器下载。是否有任何功能可以帮助我设置活动日期时间和标题? 我不应该打开EventsViewController,而是在后端执行它。有什么建议吗?

1 个答案:

答案 0 :(得分:4)

我刚设置EKEvent startDateendDate。我不知道你的意思是“在后端做这件事”......

- (BOOL)createEvent:(NSString *)title 
                 at:(NSString *)location 
           starting:(NSDate *)startDate 
             ending:(NSDate *)endDate 
           withBody:(NSString *)body 
             andUrl:(NSURL *)url
{

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

    EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
    event.title     = title;
    event.location  = location;
    event.startDate = startDate;
    event.endDate   = endDate;
    event.notes     = body;
    if (url)
        event.URL   = url;

    [event setCalendar:[eventStore defaultCalendarForNewEvents]];

    EKEventEditViewController *eventViewController = [[EKEventEditViewController alloc] init];
    eventViewController.event = event;
    eventViewController.eventStore = eventStore;
    eventViewController.editViewDelegate = self;

    [_viewController presentModalViewController:eventViewController animated:YES];

    return TRUE;
}

如果要将服务器中的日期字符串转换为NSDate个对象,NSDateFormatter通常会完成这项工作(假设服务器日期文本字符串格式正确)。例如,它可能会起作用:

NSString *sampleDate = @"7/23/12 2:30 pm";

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"M/d/y h:mm a";

NSDate *date = [formatter dateFromString:sampleDate];