我正在开发一个使用CoreLocation来管理iBeacons的iOS应用程序。我在AppDelegate和ViewController中都有信标检测,但我希望AppDelegate检测只在应用程序处于后台状态时才起作用。当用户进入或离开区域时,我会将分析发送到服务器,因此,如果在应用程序处于前台时(AppDelegate和ViewController)检测都有效,则应用程序将发送加倍的分析。
有什么解决方案可以让代码(发送分析的服务器调用)在应用程序处于前台时不运行?
发布于 2014-08-16 19:58:01
在AppDelegate中,在applicationDidEnterBackground:方法中启动信标检测,在applicationWillEnterForeground:方法中停止信标检测。
发布于 2014-08-16 21:09:42
只需在AppDelegate中实现方法来跟踪您是否在前台,如果是,则停止从AppDelegate发送分析。如下所示:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
_sendAnalytics = YES;
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
_sendAnalytics = NO;
}
像@paulw11在他的评论中建议的那样集中这种逻辑可能会使代码更干净,但如果你确实需要在两个地方发送分析,上面的方法将解决你的问题。
https://stackoverflow.com/questions/25339816
复制相似问题