在performFetchWithCompletionHandler中添加日历事件

时间:2015-03-05 11:44:28

标签: ios iphone ipad ios7 ios8

我正在尝试在

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

基于收到的用于创建日历活动的一些数据应用程序。

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

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

我所要做的只是将应用程序置于前台,然后再次按下主页按钮 - 现在我可以看到所有待处理的日历条目,这些条目应该在代码执行一段时间之后添加。

这是否已知行为?任何人都可以建议不会导致appstore拒绝的工作方法吗?

CODE:

-(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");
}

2 个答案:

答案 0 :(得分:3)

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

- (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");
}

答案 1 :(得分:3)

您似乎缺少在后台执行代码的一些步骤。

- (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];
}];

}

您还必须返回结果,以便OS根据您的返回结果再次调用相同方法的时间间隔,如下所示

-(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");
}
相关问题