0

尝试以编程方式将事件添加到 iPhone 日历。我没有收到任何错误,但是也没有任何内容添加到日历中。savedEventId(below) 返回 (null) 这是代码:

方法

添加事件:

if(accessGranted){
EKEvent *event = [EKEvent eventWithEventStore:store];
event.startDate =[NSDate date];
event.endDate = [event.startDate dateByAddingTimeInterval:60*60]; 
[event setCalendar:[store defaultCalendarForNewEvents]];
NSError *err = nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
NSString *savedEventId = event.eventIdentifier;
}

访问日历

在 viewDidLoad 中:

store = [[EKEventStore alloc] init];
accessGranted=FALSE;
[self.store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
        accessGranted=TRUE;
    }];

在 .h 文件中

    #import <EventKit/EventKit.h>

    @interface

    EKEventStore *store;
    BOOL accessGranted;

    @property(nonatomic, retain) EKEventStore *store;

在 .m 文件中

@synthesize store; 
4

1 回答 1

1

试试这个代码:

EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
    if (!granted) { return; }
    EKEvent *event = [EKEvent eventWithEventStore:store];
    event.title = @"Event Title";
    event.startDate = [NSDate date]; //today
    event.endDate = [event.startDate dateByAddingTimeInterval:60*60];  //set 1 hour meeting
    [event setCalendar:[store defaultCalendarForNewEvents]];
    NSError *err = nil;
    [store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
    NSString *savedEventId = event.eventIdentifier;  //this is so you can access this event later
}];

记得:

  • 通过“requestAccessToEntityType:completion:”请求访问用户的日历

  • 提交您的活动

于 2013-10-02T16:24:10.247 回答