iOS从后台重新检查加载位置

我正在构build一个应用程序,根据您当前的位置显示结果数据。

目前,我使用UIViewControllerviewDidLoad方法来启动CLLocationManager并获取当前位置。 一旦我的位置符合我所希望的准确性,我会向我的Web服务请求获取结果并将其转储到UITableView

我的问题是,当你closures应用程序(尽pipe它仍然在后台运行)。 如果您要开车到另一个城镇,请重新打开应用程序,数据不会更新,并继续显示旧位置的结果。

基本上,当UIViewController从后台加载,我需要能够检查用户的位置,如果他们已经移动了很长的距离,更新我的UITableView的内容。

但是,因为UIViewControllerviewDidAppear没有被触发,当你从后台加载应用程序,我不知道我可以使用哪种方法。

我知道stopMonitoringSignificantLocationChanges方法,它会在find新的位置时唤醒您的应用程序。 不过,这似乎有点OTT,因为我只需要知道应用程序加载。

有没有其他select使用stopMonitoringSignificantLocationChanges方法?

你可以注册接收来自UIApplication在-viewDidLoad通知。 您可能对UIApplicationDidBecomeActiveNotification感兴趣。 注册通知很简单。

 - (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil]; } 

-viewDidLoad我们将自己添加为UIApplicationDidBecomeActiveNotification的观察者,并指定在接收到特定通知时调用的select器。

 - (void)applicationDidBecomeActive:(NSNotification *)notification { // Kick off your CLLocationManager [self updateCurrentLocation]; } - (void)viewDidUnload { [super viewDidUnload]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil]; } 

最后,请记住,当视图卸载时,以自己的观察者身份移除自己。 以这种方式平衡你的addObserver / removeObserver调用到NSNotificationCenter是一个很好的习惯。

您可以注册您的查看通知。 对于需要跟踪应用程序状态的视图,我使用这个方便的超类。

 @implementation BackgroundAwareObject -init { if(self=[super init]) { NSNotificationCenter* center = [NSNotificationCenter defaultCenter]; [center addObserver:self selector:@selector(notifyApplicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil]; [center addObserver:self selector:@selector(notifyApplicationWillEnterForeground:) name: UIApplicationWillEnterForegroundNotification object:nil]; [center addObserver:self selector:@selector(notifyApplicationDidFinishLaunching:) name: UIApplicationDidFinishLaunchingNotification object:nil]; [center addObserver:self selector:@selector(notifyApplicationDidBecomeActive:) name: UIApplicationDidBecomeActiveNotification object:nil]; [center addObserver:self selector:@selector(notifyApplicationWillResignActive:) name: UIApplicationWillResignActiveNotification object:nil]; [center addObserver:self selector:@selector(notifyApplicationDidReceiveMemoryWarning:) name: UIApplicationDidReceiveMemoryWarningNotification object:nil]; [center addObserver:self selector:@selector(notifyApplicationWillTerminate:) name: UIApplicationWillTerminateNotification object:nil]; } return self; } -(void)notifyApplicationDidEnterBackground:(NSNotification*)n { [self applicationDidEnterBackground:[UIApplication sharedApplication]]; } -(void)notifyApplicationWillEnterForeground:(NSNotification*)n { [self applicationWillEnterForeground:[UIApplication sharedApplication]]; } -(void)notifyApplicationDidFinishLaunching:(NSNotification*)n { [self application:[UIApplication sharedApplication] didFinishLaunchingWithOptions: [n userInfo]]; } -(void)notifyApplicationDidBecomeActive:(NSNotification*)n { [self applicationDidBecomeActive:[UIApplication sharedApplication]]; } -(void)notifyApplicationWillResignActive:(NSNotification*)n { [self applicationWillResignActive:[UIApplication sharedApplication]]; } -(void)notifyApplicationDidReceiveMemoryWarning:(NSNotification*)n { [self applicationDidReceiveMemoryWarning:[UIApplication sharedApplication]]; } -(void)notifyApplicationWillTerminate:(NSNotification*)n { [self applicationWillTerminate:[UIApplication sharedApplication]]; } -(void)configurationChanged { // User has update application configuration panel } - (void)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. } - (void)applicationWillResignActive:(UIApplication *)application { /* Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. */ } - (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, called instead of applicationWillTerminate: when the user quits. */ _background = YES; } - (void)applicationWillEnterForeground:(UIApplication *)application { /* Called as part of transition from the background to the active state: here you can undo many of the changes made on entering the background. */ } - (void)applicationDidBecomeActive:(UIApplication *)application { /* Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. */ _background = NO; } /** applicationWillTerminate: saves changes in the application's managed object context before the application terminates. */ - (void)applicationWillTerminate:(UIApplication *)application { } // try to clean up as much memory as possible. next step is to terminate app - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application { } -(void)dealloc { [[NSNotificationCenter defaultCenter]removeObserver:self]; [super dealloc]; } @end 

在您的AppDelegate.m您必须预先定义此方法(如在Xcode中启动项目时最初创build的模板中)

 - (void)applicationDidBecomeActive:(UIApplication *)application { /* Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. */ } 

正如描述所说,当应用程序即将变为活动状态时,iOS自动调用此方法。 在这里你可以得到最新的位置做进一步的处理。