【问题标题】:adding calendar event in performFetchWithCompletionHandler在 performFetchWithCompletionHandler 中添加日历事件
【发布时间】:2015-05-06 18:41:50
【问题描述】:

我正在尝试在

中添加日历事件
-(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");
}

【问题讨论】:

  • 你用的是什么代码?
  • 您是否调用了将数据添加到日历的权限
  • 是的。该应用程序具有权限。也添加代码。
  • 你要闹吗?

标签: ios iphone ipad ios7 ios8


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-02
      • 2017-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多