14

我有将事件添加到日历的代码,但它没有添加。

-(void)event
{
    EKEventStore *eventStore = [[EKEventStore alloc] init];

    EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
    event.title     = @"Event";


    NSDateFormatter *tempFormatter = [[NSDateFormatter alloc]init];
    [tempFormatter setDateFormat:@"dd.MM.yyyy HH:mm"];


    NSString *dateandtime =[NSString stringWithFormat:@"%@%@%@",datestring,@" ",starttimestring];
    NSString *dateandtimeend =[NSString stringWithFormat:@"%@%@%@",datestring,@" ",endtimestring];



    event.startDate = [tempFormatter dateFromString:dateandtime];
    event.endDate = [tempFormatter dateFromString:dateandtimeend];


    [event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -60.0f * 24]];
    [event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -15.0f]];

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

从 XML 我得到这种格式的日期和时间:

日期字符串:28.10.2012

开始时间字符串:15:00

4

2 回答 2

24

您是在 iOS 6 模拟器上还是在装有 iOS 6 的设备上?如果是这样,您需要先询问用户使用事件存储的权限,然后才能将项目保存到其中。

基本上,如果 requestAccessToEntityType:completion: 选择器在您的事件存储对象上可用,则调用该方法并提供在用户授予权限时执行的代码块,然后您将在该块中保存事件。

首先将 EventKit 框架添加到您的项目中,并且不要忘记包含导入:

#import <EventKit/EventKit.h>

这是我使用的对我有用的代码片段:

EKEventStore *eventStore = [[EKEventStore alloc] init];
if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
{
    // the selector is available, so we must be on iOS 6 or newer
    [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (error)
            {
                // display error message here
            }
            else if (!granted)
            {
                // display access denied error message here
            }
            else
            {
                // access granted
                // ***** do the important stuff here *****
            }
        });
    }];
}
else
{
    // this code runs in iOS 4 or iOS 5
    // ***** do the important stuff here *****
}

[eventStore release];

这是我在这个主题上写的一篇博文:

http://www.dosomethinghere.com/2012/10/08/ios-6-calendar-and-address-book-issues/

于 2012-10-26T15:23:14.307 回答
2

1)添加Eventkit框架和#import <EventKit/EventKit.h>

2)

 -(void)syncWithCalendar {
    EKEventStore *store = [EKEventStore new];
    [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
        if (!granted) { return; }
        EKEvent *event = [EKEvent eventWithEventStore:store];
        event.title = @"Event Title Testing"; //give event title you want
        event.startDate = [NSDate date];
        event.endDate = [event.startDate dateByAddingTimeInterval:60*60];
        event.calendar = [store defaultCalendarForNewEvents];
        NSError *err = nil;
        [store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
    }];
}

3) 调用函数

[self syncWithCalendar];
于 2016-04-25T06:55:30.750 回答