在 performFetchWithCompletionHandler 中添加日历事件
adding calendar event in performFetchWithCompletionHandler
我正在尝试在
中添加日历事件
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
基于应用收到的用于创建日历事件的一些数据。
我的问题是: 创建日历事件的代码运行没有任何问题,但是 事件 没有显示在日历上。当我在我的 "add event" 代码执行后将应用程序带回前台时,我可以看到当我按下主页按钮时添加的日历事件
此外,如果我的代码在应用程序处于后台时运行 2-3 次,事件条目似乎在代码执行后保持 "pending" 状态,并且当应用程序进入前台时,所有事件都会立即添加.
我所要做的只是将应用程序调到前台一次,然后再次按下主页按钮 - 现在我可以看到所有待处理的日历条目,一旦代码早些时间执行,这些条目就应该被添加。
这是已知行为吗?谁能提出不会导致应用商店拒绝的解决方法?
代码:
-(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");
}
您似乎缺少一些在后台执行代码的步骤。
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
UIBackgroundTaskIdentifier backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:backgroundTask];
}];
}
您还必须 return 结果,因此 OS 根据您的 return 结果决定再次调用相同方法的时间间隔,如下所示
-(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);
completionHandler(UIBackgroundFetchResultNoData);
}
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");
completionHandler(UIBackgroundFetchResultNoData);
} else {
NSLog(@"successfully added");
completionHandler(UIBackgroundFetchResultNewData);
}
});
}
}];
NSLog(@"background fetch");
}
我正在尝试在
中添加日历事件-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
基于应用收到的用于创建日历事件的一些数据。
我的问题是: 创建日历事件的代码运行没有任何问题,但是 事件 没有显示在日历上。当我在我的 "add event" 代码执行后将应用程序带回前台时,我可以看到当我按下主页按钮时添加的日历事件
此外,如果我的代码在应用程序处于后台时运行 2-3 次,事件条目似乎在代码执行后保持 "pending" 状态,并且当应用程序进入前台时,所有事件都会立即添加.
我所要做的只是将应用程序调到前台一次,然后再次按下主页按钮 - 现在我可以看到所有待处理的日历条目,一旦代码早些时间执行,这些条目就应该被添加。
这是已知行为吗?谁能提出不会导致应用商店拒绝的解决方法?
代码:
-(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");
}
您似乎缺少一些在后台执行代码的步骤。
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
UIBackgroundTaskIdentifier backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:backgroundTask];
}];
}
您还必须 return 结果,因此 OS 根据您的 return 结果决定再次调用相同方法的时间间隔,如下所示
-(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);
completionHandler(UIBackgroundFetchResultNoData);
}
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");
completionHandler(UIBackgroundFetchResultNoData);
} else {
NSLog(@"successfully added");
completionHandler(UIBackgroundFetchResultNewData);
}
});
}
}];
NSLog(@"background fetch");
}