gpt4 book ai didi

ios - 在 performFetchWithCompletionHandler 中添加日历事件

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:15:01 31 4
gpt4 key购买 nike

我正在尝试在

中添加日历事件
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

基于应用收到的用于创建日历事件的一些数据。

我的问题是:创建日历事件的代码运行没有任何问题,但是事件没有显示在日历上。当我在执行“添加事件”代码后将应用程序带回前台时,我可以在按下主页按钮时看到添加的日历事件

此外,如果我的代码在应用程序处于后台时运行 2-3 次,事件条目似乎在代码执行后保持“待定”状态,并且当应用程序进入前台时,所有事件都会立即添加。

我所要做的只是将应用程序调到前台一次,然后再次按下主页按钮 - 现在我可以看到所有挂起的日历条目,一旦代码早些时候执行,这些条目就应该被添加。

这是已知行为吗?谁能建议不会导致应用商店拒绝的解决方法?

代码:

-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
EKEventStore *store = [[EKEventStore alloc] init];

[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
{
if (error)
NSLog(@"EKEventStore error = %@", error);

if (granted)
{
NSLog(@"EKEvent *event ");

EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = @"test";
event.startDate = [NSDate date];
event.endDate = [[NSDate date] dateByAddingTimeInterval:600];
[event setCalendar:[store defaultCalendarForNewEvents]];

NSError *err = nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];

if (err)
{
NSLog(@"not added");
}
else
{
NSLog(@"successfully added");
}
}
}];

NSLog(@"background fetch");
}

最佳答案

您需要在主线程中创建事件。

- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandle {
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (error) {
NSLog(@"EKEventStore error = %@", error);
}
if (granted) {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"EKEvent *event ");

EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = @"test";
event.startDate = [NSDate date];
event.endDate = [[NSDate date] dateByAddingTimeInterval:600];

[event setCalendar:[store defaultCalendarForNewEvents]];
NSError *err = nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];

if (err) {
NSLog(@"not added");
} else {
NSLog(@"successfully added");
}
});
}
}];
NSLog(@"background fetch");
}

关于ios - 在 performFetchWithCompletionHandler 中添加日历事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28877118/

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