Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在performFetchWithCompletionHandler中添加日历事件_Ios_Iphone_Ipad_Ios7_Ios8 - Fatal编程技术网

Ios 在performFetchWithCompletionHandler中添加日历事件

Ios 在performFetchWithCompletionHandler中添加日历事件,ios,iphone,ipad,ios7,ios8,Ios,Iphone,Ipad,Ios7,Ios8,我正在尝试在中添加日历事件 -(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler 基于应用程序收到的用于创建日历事件的某些数据 我的问题是:创建日历事件的代码运行时没有任何问题,但是事件不会显示在日历上。当我在“添加事件”代码执行后将应用程序带回前台时,我可以在按下home按钮时看

我正在尝试在中添加日历事件

-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
基于应用程序收到的用于创建日历事件的某些数据

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

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

我所要做的就是将应用程序放在前台一次,然后再按home按钮-现在我可以看到所有挂起的日历项,这些日历项应该在代码之前执行一段时间后添加

这是已知的行为吗?是否有人建议采取不会导致appstore拒绝的解决方案

代码:


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

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

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

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

您正在使用什么代码?您是否调用了“获取向日历添加数据的权限”是。该应用程序具有相应的权限。还添加了代码。你想创建alaram吗?
-(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");
}