首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在performFetchWithCompletionHandler中添加日历事件

在performFetchWithCompletionHandler中添加日历事件
EN

Stack Overflow用户
提问于 2015-03-05 11:44:28
回答 2查看 193关注 0票数 2

中添加日历事件。

代码语言:javascript
运行
复制
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

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

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

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

我所要做的就是把这个应用程序放在前台,然后再按“主页”按钮--现在我可以看到所有挂起的日历条目,这些条目应该在代码执行之前一段时间就添加了。

这是已知的行为吗?有谁能建议一些不会导致应用程序被拒绝的解决办法?

代码:

代码语言:javascript
运行
复制
-(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");
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-03-24 01:28:18

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

代码语言:javascript
运行
复制
- (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");
}
票数 3
EN

Stack Overflow用户

发布于 2015-03-24 11:38:40

您似乎遗漏了在后台执行代码的一些步骤。

代码语言:javascript
运行
复制
- (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根据返回结果决定再次调用相同方法的时间间隔,如下所示

代码语言:javascript
运行
复制
-(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");
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28877118

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档