gpt4 book ai didi

iphone - 添加事件未显示在 iPhone 日历中

转载 作者:行者123 更新时间:2023-11-29 03:36:17 24 4
gpt4 key购买 nike

尝试以编程方式将事件添加到 iPhone 日历。我没有收到任何错误,但是,日历中也没有添加任何内容。saveEventId(下面)返回(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; 

最佳答案

试试这个代码:

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:”请求访问用户的日历

  • 提交您的事件

关于iphone - 添加事件未显示在 iPhone 日历中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19141427/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com