在后台接收并回复EKEventStoreChangedNotification?

alb*_*osh 0 ios eventkit ios7

我想知道如果在iOS7中,使用新的API,它最终可以在后台响应通知,在我的情况下,我有以下观察者:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(storeChanged:)
                                             name:EKEventStoreChangedNotification
                                           object:eventStore];
Run Code Online (Sandbox Code Playgroud)

我完全收到通知但我需要运行应用程序以便调用选择器.我已经浏览了回复,他们说这是不可能的,但不确定他们是否具体指的是iOS7.

有什么想法吗?

谢谢!

Kev*_*vin 5

只有当您的应用程序到达前台时,才会触发EKEventStoreChangedNotification.但是,如果您想在后台调用storeChanged:方法,并且因此已经更新了UI,那么您需要在应用程序中添加背景提取功能.

<key>UIBackgroundModes</key>
<array>
    <string>fetch</string>
</array>
Run Code Online (Sandbox Code Playgroud)

在您的app delegate didFinishLaunchingWithOptions方法中添加该行

[application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
Run Code Online (Sandbox Code Playgroud)

这可以确保您的应用实际调用您的后台提取,因为默认间隔永远不会.此最小密钥是确保iOS处理何时调用后台获取方法的键.如果您不希望它尽可能频繁地触发,您可以设置自己的最小间隔.

最后在app delegate中实现后台获取方法:

- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
 [self storeChanged:nil];
 completionHandler(UIBackgroundFetchResultNewData);
}
Run Code Online (Sandbox Code Playgroud)

您可以在调试时使用Debug> Simulate Background Fetch在Xcode中进行测试.