iOS从后台重新检查加载位置 [英] iOS re-check location on load from background
问题描述
目前,我正在使用 viewDidLoad
UIViewController
的方法来启动 CLLocationManager
并获取当前位置。一旦我的位置与我想要的精度相匹配,我就会向我的Web服务请求获取结果并将其转储到 UITableView
中。
我的问题是关闭应用程序时(尽管它仍在后台运行)。如果您要开车去另一个城镇,重新打开应用程序,数据不会更新,并继续显示您旧位置的结果。
基本上当从后台加载 UIViewController
,我需要能够检查用户的位置,如果它们移动了很长的距离,请更新我的 UITableView
。
然而,因为的
在从后台加载应用程序时不会触发,我不确定可以使用哪种方法。 viewDidAppear
> UIViewController
我是意识到 stopMonitoringSignificantLocationChanges
方法会在找到新位置时唤醒您的应用程序。然而,这似乎有点OTT,因为我只需要知道一旦应用程序已经加载。
有没有其他选择使用 stopMonitoringSignificantLocationChanges
您可以注册以接收 -viewDidLoad <从UIApplication的通知/ code>。您可能对
UIApplicationDidBecomeActiveNotification
感兴趣。注册通知很简单。
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive :) name:UIApplicationDidBecomeActiveNotification object:nil];
在 -viewDidLoad
我们添加自己作为 UIApplicationDidBecomeActiveNotification
并指定当接收到该特定的通知时要调用的选择器。
的观察者
- (void)applicationDidBecomeActive:(NSNotification *)notification
{
//启动CLLocationManager
[self updateCurrentLocation];
}
- (void)viewDidUnload
{
[super viewDidUnload];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
}
最后,请记住在视图卸载时以通知的观察者的身份。以这种方式平衡你的addObserver / removeObserver调用到NSNotificationCenter是一个好习惯。
I'm building an app which displays result data based on your current location.
At the moment, I'm using the viewDidLoad
method of a UIViewController
to start the CLLocationManager
and getting the current location. Once I have the location that matches the accuracy I desire, I do a request to my web service to get the results and dump it into a UITableView
.
My problem is that when you close the app (although it's still running in the background). If you were to drive to another town, re-open the app, the data doesn't get updated and continues to show the results from your old location.
Basically when the UIViewController
loads from the background, I need to be able to check the users location, and if they have moved a significant distance, update the contents of my UITableView
.
However, because the viewDidAppear
of the UIViewController
isn't triggered when you load the app from the background, I'm not sure which method I can use.
I am aware of the stopMonitoringSignificantLocationChanges
method which wakes up your app when a new location is found. However, this seems a little OTT because I only need to know once the app has loaded.
Is there any alternative to using the stopMonitoringSignificantLocationChanges
method?
You could register to receive notifications from UIApplication in -viewDidLoad
. You may be interested in UIApplicationDidBecomeActiveNotification
. Registering for notifications is easy.
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
}
In -viewDidLoad
we add ourselves as an observer of the UIApplicationDidBecomeActiveNotification
and specify a selector to be invoked when that particular notification is received.
- (void)applicationDidBecomeActive:(NSNotification *)notification
{
// Kick off your CLLocationManager
[self updateCurrentLocation];
}
- (void)viewDidUnload
{
[super viewDidUnload];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
}
Finally, remember to remove yourself as an observer for this notification when the view unloads. It's good practice to balance your addObserver/removeObserver calls to NSNotificationCenter in this way.
这篇关于iOS从后台重新检查加载位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!