简体   繁体   English

有时仅在iOS上更新后台位置

[英]Update location in background on iOS only sometimes

I'm working on an application that uses location. 我正在使用一个使用位置的应用程序。 The application can save a route on a map and if the user wants to record a route, the app have to work on background storing the user location to show it after that on the map. 应用程序可以在地图上保存路线,如果用户想要记录路线,则应用程序必须处理存储用户位置的背景,然后在地图上显示该位置。

To save the location on background I used the property NSLocationAlwaysUsageDescription defined on the plist of the application 为了在背景上保存位置,我使用了在应用程序的plist上定义的属性NSLocationAlwaysUsageDescription

应用程序plist上的NSLocationAlwaysUsageDescription

My problem is that it's possible that user never wants to record any routes, but because of the way I'm defining that the app have to store location on background, my app seems to work always on background and it wastes so much battery even when could be not necessary to work on background. 我的问题是,用户可能永远不想记录任何路线,但由于我定义应用程序必须在后台存储位置的方式,我的应用程序似乎总是在后台工作,它甚至浪费了很多电池可能没有必要在背景上工作。

I also start and stop the update of the location on my viewDidAppear and viewDidDisappear methods to avoid the update of the location when I'm not on the map screen: 我也开始和停止更新viewDidAppear和viewDidDisappear方法上的位置,以避免在我不在地图屏幕上时更新位置:

    - (void)viewDidAppear:(BOOL)animated
{
    ...

    [[SBLocationManager sharedInstance] startUpdatingLocation];
    ...
}

- (void)viewDidDisappear:(BOOL)animated
{
    [[SBLocationManager sharedInstance] stopUpdatingLocation];
    [self.stationProvider stopProviding];
}

I've been looking about that but I don't find a way to set the background location on/off when I want. 我一直在寻找这个,但我没有找到一种方法来打开/关闭我想要的背景位置。 Do you know if there is a way to "enable/disable" the location on background while you're on the app to let the user configurate it? 你知道在应用程序上有没有办法在背景上“启用/禁用”该位置以让用户配置它?

Thanks so much for your help :) See you here! 非常感谢你的帮助:)在这里见!


Adding this as an answer, so that if anyone else has the similar query they can refer. 添加此作为答案,以便如果其他人有类似的查询,他们可以参考。

Here is the similar question Periodic iOS background location updates 这是类似的问题定期iOS背景位置更新

To prevent battery life, it requires to turn on - off location updates as per our reqiurements. 为了防止电池寿命,它需要根据我们的要求打开 - 关闭位置更新。 Also there are few properties, desiredAccuracy and distanceFilter can be set in such a way so that it won't call location services frequently. 此外,还有一些属性,desiredAccuracy和distanceFilter可以这样设置,以便它不会经常调用位置服务。 (As mentioned in given link) (如给定链接中所述)

Hope this helps. 希望这可以帮助。

I've been doing some changes according to the answers I got on the post that gave me a little improvement of the battery waste on my app, I have to test if is enough or if I can change something else, but here are the improvements I did, if it could be useful for someone: 我根据我在帖子上得到的答案做了一些改变,这让我对我的应用程序上的电池浪费有了一些改进,我必须测试是否足够或者我是否可以改变别的东西,但这里有改进我做了,如果它对某人有用:

I changed the startUpdatingLocation and stopUpdatingLocation on viewDidAppear and viewDidDisappear for that: 我更改了viewDidAppear和viewDidDisappear上的startUpdatingLocation和stopUpdatingLocation:

- (void)viewDidAppear:(BOOL)animated
{
    ...

    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [self.locationManager setDistanceFilter:kCLDistanceFilterNone];

    ...
}

- (void)viewDidDisappear:(BOOL)animated
{
    ...
        [self.locationManager setDesiredAccuracy:kCLLocationAccuracyThreeKilometers];
    [self.locationManager setDistanceFilter:99999];
    ...
}


I added two more properties to my locationManager to save battery: 我在locationManager中添加了两个属性以节省电池电量:

_locationManager.pausesLocationUpdatesAutomatically = YES;
_locationManager.activityType = CLActivityTypeOtherNavigation;


And I found an error that was preventing location be stopped when you had close the application from the map view, because viewDidDisappear and stopUpdatingLocation wasn't get called. 当我从地图视图中关闭应用程序时,我发现了一个阻止位置停止的错误,因为未调用viewDidDisappear和stopUpdatingLocation。 Because of that, I added that on : 因此,我补充说:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [self.locationManager stopUpdatingLocation];
    ...
}

I hope this will be useful for you too :) Thanks everyone for the help! 我希望这对你也有用:)谢谢大家的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM