0

我有一个大问题。使用 iOS 7.1,即使应用程序关闭(不是在后台,而是完全关闭),也可以监控信标区域的进入/退出。

但我注意到只有当信标代表在主控制器中时才有可能,而不是在其他控制器中时(例如以模态方式调用)。

例如,我的应用程序是使用以下内容构建的:

appDelegate --> firstController --> secondController

如果我将信标管理器及其代表放在第一个控制器中,它们也会在应用程序关闭的情况下被调用,而如果我在第二个控制器(显示为模态视图控制器)中执行相同操作,它们不会被调用(它们仅在前台或背景)。

如何解决这个问题?

4

2 回答 2

0

解决此问题的一种常见方法是专门使用您的 AppDelegate 作为 CLLocationManager 委托。在 AppDelegate 的回调方法中,您可以检查在任何给定时间哪个 ViewController(如果有)处于活动状态,并根据需要从回调方法调用该 ViewController。

为此,您必须在 AppDelegate 上为每个使用 CoreLocation 服务的 ViewController 创建一个属性:

@interface MyAppDelegate : UIResponder <CLLocationManagerDelegate>
@property (strong, nonatomic) FirstViewController *firstViewController;
@property (strong, nonatomic) SecondViewController *secondViewController;
@end

然后在加载 ViewController 时设置相应的属性。像这样:

- (void)viewDidLoad
{
    [super viewDidLoad];
    MyAppDelegate *appDelegate = (MyAppDelegate *) [[UIApplication sharedApplication] delegate];
    appDelegate.firstViewController = self;
    ...
}

完成此操作后,您可以在 MyAppDelegate 中专门注册回调,但将它们传递给单独的 ViewController,如下所示:

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
    if (self.firstViewController != Nil) {
        [self locationManager:manager didEnterRegion:region]; 
    }
}
于 2014-05-12T16:10:13.883 回答
0

我在处理 CLLocationManager 时正在做的事情如下:

  • 我创建了一个实现单例模式的类,该模式充当 CLLocationManager 回调的委托。
  • 然后我创建一个协议,该协议使用我想要转发到特定视图控制器的委托方法
  • 我在所需的视图控制器中实现协议方法。
  • 在 AppDelegate(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中,我第一次实例化单例,以便注册 CLLocationManager 和 UUID
  • 当来自 CLLocationManager 的回调到达我注册的视图控制器之一时,我检查我的应用程序当前是否处于活动状态,然后相应地处理回调

    UIApplicationState state = [application applicationState];
    if (state == UIApplicationStateActive) {
    // Application is in foreground
    }
    
于 2014-05-13T22:19:30.697 回答