我想做以下
以下是我的表现:
APPDelegate中的:
在applicationDidFinishLaunching中:
if ([CLLocationManager locationServicesEnabled])
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
_startLocation = nil;
self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;
self.locationManager.distanceFilter=500;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
// Set Property
self.myLocation = newLocation;
BOOL isInBackground = NO;
if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground)
{
isInBackground = YES;
}
// Handle location updates as normal, code omitted for brevity.
// The omitted code should determine whether to reject the location update for being too
// old, too close to the previous one, too inaccurate and so forth according to your own
// application design.
if (isInBackground)
{
[self sendBackgroundLocationToServer:newLocation];
}
if(newLocation.horizontalAccuracy <= 100.0f)
{
[self.locationManager stopMonitoringSignificantLocationChanges];
}
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[self.locationManager stopUpdatingLocation];
[self.locationManager startMonitoringSignificantLocationChanges];
// 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, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
if(self.locationManager == nil)
{
self.locationManager = [[CLLocationManager alloc] init];
}
[self.locationManager stopMonitoringSignificantLocationChanges];
// 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.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
self.locationManager = nil;
[self.locationManager stopUpdatingLocation];
[self.locationManager stopMonitoringSignificantLocationChanges];
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
ViewController中的:
- (void) startLocationChanges {
if ([CLLocationManager locationServicesEnabled])
{
PLAppDelegate *appDelegate = (PLAppDelegate *) [[UIApplication sharedApplication] delegate];
if(appDelegate.locationManager == nil)
{
self.locationManager = [[CLLocationManager alloc] init];
}
else {
self.locationManager = appDelegate.locationManager;
}
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
self.myLocation = newLocation;
[self uploadLocationCoordinates];
if(newLocation.horizontalAccuracy <= 100.0f)
{
NSLog(@"stop updating location");
[self.locationManager stopUpdatingLocation];
}
//_horizontalAccuracy.text = currentHorizontalAccuracy;
}
我这样做是对的吗?当有人关闭应用程序时,似乎位置服务仍然存在。
答案 0 :(得分:2)
从设计的角度来看,我发现最好将位置管理器包装在单个类(example)中。你在这做什么:
if ([CLLocationManager locationServicesEnabled])
{
PLAppDelegate *appDelegate = (PLAppDelegate *) [[UIApplication sharedApplication] delegate];
if(appDelegate.locationManager == nil)
{
self.locationManager = [[CLLocationManager alloc] init];
}
else {
self.locationManager = appDelegate.locationManager;
}
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];
}
...非常混乱:您可能正在复制位置管理器对象或只是引用它(并且代码不清楚在没有看到头文件的情况下发生了什么)并且您通常不应该切换代理位置经理。
使用单例类将为您提供一个处理与位置相关的事物的地方,您的视图控制器和其他类可以随时请求这些数据。或者,您可以使用委托模式在单例中存储对每个位置感知类(例如视图控制器,应用程序委托)的引用,并在收到locationManager:didUpdateToLocation
等回调时广播到这些类。
我希望有所帮助 - 看看我添加的链接,看看我的意思:单身模式在这里使用是完美的,因为你只需要在任何一个地方使用一个位置数据源时间。
答案 1 :(得分:2)
您的代码似乎没问题,但如果您注册了位置更改,它将在后台保持活动状态。如果您想暂停位置更改,有几种方法,我通常会在AppDelegate
中停止它们。
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSLog(@"Application entering background");
CLLocationManager *locMan = [[CLLocationManager alloc] init];
[locMan stopMonitoringSignificantLocationChanges];
[locMan stopUpdatingLocation];
}
- (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.
CLLocationManager *locMan = [[CLLocationManager alloc] init];
[locMan startMonitoringSignificantLocationChanges];
}
答案 2 :(得分:0)
如果您在iphone中按中心按钮关闭应用程序,则会调用此方法
- (void)applicationWillEnterForeground:(UIApplication *)application