在后台接收并回复EKEventStoreChangedNotification? [英] Receive and respond to EKEventStoreChangedNotification in the background?

查看:233
本文介绍了在后台接收并回复EKEventStoreChangedNotification?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was wondering if in iOS7, with the new API's it was finally possible to respond to a notification in the background, in my case, I have the following observer:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(storeChanged:)
                                             name:EKEventStoreChangedNotification
                                           object:eventStore];

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

I am receiving the notification perfectly but i need to run the app so that the selector gets called. I've browsed through the response and they say it's not possible but not sure if they where referring to iOS7 specifically.

有什么想法吗?

谢谢!

推荐答案

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

The EKEventStoreChangedNotification will only fire when your app comes to the foreground. However if you want to call your storeChanged: method in the background, and thus having the UI already updated on coming to foreground again, you need to add the Background Fetch capability to your app.

<key>UIBackgroundModes</key>
<array>
    <string>fetch</string>
</array>

在你的app appate didFinishLaunchingWithOptions方法中添加行

In your app delegate didFinishLaunchingWithOptions method add the line

[application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];

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

This ensures your app actually calls your background fetch, as the default interval is never. This minimum key is the key that ensures iOS handles when to call your background fetch method. You can set your own minimum interval if you don't want it to fire as often as possible.

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

Finally implement the background fetch method in your app delegate:

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

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

You can test in Xcode while debugging from Debug > Simulate Background Fetch.

这篇关于在后台接收并回复EKEventStoreChangedNotification?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆