在iOS中将事件添加到生日日历

时间:2012-11-17 11:39:17

标签: iphone ios calendar add

我写了这段代码,但它不起作用,你能帮我吗?我坚持了最后2个小时。

- (IBAction)addBirthday:(id)sender {

    NSString *name = self.textField.text;
    NSDate *date = [self.datePicker date];

    EKEventStore *eventStore = [[EKEventStore alloc] init];
    [eventStore calendarsForEntityType:EKEntityTypeEvent];

    EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
    event.title = [NSString stringWithFormat:@"%@’s Birthday", name];

    event.startDate = date;
    event.endDate   = date;

    NSArray *calendars = [eventStore
                          calendarsForEntityType:EKEntityTypeEvent];

    NSLog(@"ARRAY: %@", [calendars objectAtIndex:0]);

    [event setCalendar: [calendars objectAtIndex:0]];
    NSError *err;
    [eventStore saveEvent:event span:EKSpanThisEvent error:&err];
}

1 个答案:

答案 0 :(得分:1)

生日日历可能只读