中添加日历事件。
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler基于为创建日历事件而收到的一些数据应用程序。
我的问题是:创建日历事件的代码运行时没有任何问题,但是,事件不会出现在日历上。在执行“添加事件”代码后,当我将应用程序带回前台时,我可以在按“home”按钮时看到添加的日历事件。
另外,如果我的代码在后台运行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");
}发布于 2015-03-24 01:28:18
您需要在主线程中创建事件。
- (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");
}发布于 2015-03-24 11:38:40
您似乎遗漏了在后台执行代码的一些步骤。
- (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");
}https://stackoverflow.com/questions/28877118
复制相似问题