I've been trying to figure out how to get the user's location when the app is terminated like how the app Moves does it. The only way I know of doing so is with Significant Location Changes. However with significant location changes, the app only gets woken up either every 500 meters and only if it has been around 5 minutes past the last update.
我一直在试图弄清楚如何在应用程序终止时获取用户的位置,比如应用程序是如何运行的。我知道这样做的唯一方法是进行重要的位置更改。然而,由于位置发生了重大变化,这款应用程序每隔500米就会被叫醒一次,而且只有在上次更新后大约5分钟后才会被叫醒。
According to Apple:
根据苹果公司:
Apps can expect a notification as soon as the device moves 500 meters or more from its previous notification. It should not expect notifications more frequently than once every five minutes. If the device is able to retrieve data from the network, the location manager is much more likely to deliver notifications in a timely manner.
一旦设备移动500米以上,应用程序就会收到通知。它不应该期望每五分钟收到一次通知。如果设备能够从网络检索数据,那么位置管理器就更有可能及时交付通知。
In the Moves app, even with the app is terminated, it is able to retrieve the user's location very accurately without having much drainage in battery. It also doesn't seem to have background location turned on as it's reason in the battery usage list only shows "Background Activity". So I'm wondering how an app like Moves is doing this. Any help would be great! Thanks.
在move app中,即使app已经被终止,它也可以非常准确地获取用户的位置,而不需要耗费大量的电量。它似乎也没有背景位置打开,因为它的原因在电池使用列表只显示“后台活动”。我想知道像move这样的应用是怎么做到的。任何帮助都将是伟大的!谢谢。
6 个解决方案
#1
4
You can register for regions in 500m area and register for notification region notification in them. When it reaches one if them, your app is launched in background. Then, re-register for new ones keeping that location at center.
你可以在500米范围内的地区注册,并在其中注册通知区通知。当达到1时,你的应用会在后台启动。然后,重新注册,以保持该位置在中心。
From Apple's Documentation:
从苹果的文档:
Apps that support background execution may be relaunched by the system to handle incoming events. If an app is terminated for any reason other than the user force quitting it, the system launches the app when one of the following events happens:
支持后台执行的应用程序可以由系统重新启动来处理传入的事件。如果一个应用程序由于除用户强制退出之外的任何原因被终止,系统会在以下事件发生时启动该应用程序:
For location apps:
定位程序:
The system receives a location update that meets the app’s configured criteria for delivery.
系统接收一个位置更新,满足应用程序配置的交付标准。
The device entered or exited a registered region. (Regions can be geographic regions or iBeacon regions.)
设备进入或退出注册区域。(区域可以是地理区域,也可以是iBeacon区域。)
#2
3
In case you need location updates frequently but still want to save battery life, try this -
如果你需要经常更新位置,但仍然想节省电池寿命,试试这个-
- Add necessary permissions for using location in background.
- 为在后台使用位置添加必要的权限。
- Start the updates using
startUpdatingLocation
- 使用startUpdatingLocation启动更新
- Start deferring updates when in background using
allowDeferredLocationUpdatesUntilTraveled:timeout:
Specify time and distance filters in this method. - 在后台使用allowdeferredlocationupdatesuntiltraveling:timeout: timeout:在此方法中指定时间和距离过滤器。
- Make sure you read the docs here and specify all the properties correctly to get it working.
- 确保您阅读了这里的文档,并正确地指定了所有属性以使其工作。
#3
2
The only solution is to use background mode with CoreLocation. If your app requires iOS 8, the system will relaunch your app, even if force quit by the user. This is documented in Apple Docs:
唯一的解决方案是使用带有CoreLocation的后台模式。如果你的应用需要ios8,系统会重新启动你的应用程序,即使用户退出。这是在苹果文档中记录的:
In most cases, the system does not relaunch apps after they are force quit by the user. One exception is location apps, which in iOS 8 and later are relaunched after being force quit by the user. In other cases, though, the user must launch the app explicitly or reboot the device before the app can be launched automatically into the background by the system.
在大多数情况下,系统不会在用户强制退出后重新启动应用程序。一个例外是位置应用程序,在ios8和之后,它会在用户强制退出后重新启动。不过,在其他情况下,用户必须显式地启动应用程序或重启设备,然后系统才能自动将应用程序启动到后台。
#4
1
in iOS 8 they are new steps that we need to pay attention to in order make make the location fetching work. The first step is to add either one or two keys into the project’s .plist depending on the main functionality of the app. The two keys are NSLocationWhenInUseUsageDescription and NSLocationAlwaysUsageDescription, you will then need to add a String that explains to the user why does the app needs to access his location, something among the lines of “This app uses location in the background/foreground because of A, B and C”. Each of these Strings has a corresponding authorization method that needs to be called, WhenInUse or Alway (i.e. Background).
在iOS 8中,它们是我们需要注意的新步骤,以使位置获取工作。第一步是将一个或两个键添加到项目的.plist取决于应用程序的主要功能。这两个键是NSLocationWhenInUseUsageDescription NSLocationAlwaysUsageDescription,然后,您将需要添加一个字符串,向用户解释为什么这个应用程序需要访问他的位置,线的东西”这个程序在后台使用位置/前景是因为,B和C”。每一个字符串都有一个相应的授权方法,需要在使用或总是(例如,后台)时调用该方法。
- (void)startStandardUpdates
{
// Create the location manager if this object does not
// already have one.
if (nil == locationManager)
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
// Set a movement threshold for new events.
locationManager.distanceFilter = 500; // meters
[locationManager startUpdatingLocation];
}
#5
0
From my current experience, you have 3 options to make this work. The first one is to have a silent notification which requires an effort done by the backend. The silent notification is some sort of a push notification but it notifies your app without an alert. The second solution is background fetches. Background fetches don't require a backend solution but sadly you can't control the intervals needed to instantiate it. The third one is having a scheduled local notifications with no messages.
根据我目前的经验,你有3个选择。第一个是要有一个无声的通知,这需要后台的努力。无声通知是某种推送通知,但它在没有警告的情况下通知你的应用程序。第二个解决方案是后台取回。后台取回不需要后端解决方案,但遗憾的是您不能控制实例化它所需的间隔。第三个是有一个计划好的本地通知,没有消息。
#6
0
From the docs
从文档
Tracking the User’s Location
跟踪用户的位置
There are several ways to track the user’s location in the background, most of which do not actually require your app to run continuously in the background:
有几种方法可以跟踪用户在后台的位置,其中大部分实际上并不要求应用程序在后台连续运行:
The significant-change location service (Recommended) Foreground-only location services Background location services
后台执行
And for the details - Getting the User’s Location
以及细节——获取用户的位置
#1
4
You can register for regions in 500m area and register for notification region notification in them. When it reaches one if them, your app is launched in background. Then, re-register for new ones keeping that location at center.
你可以在500米范围内的地区注册,并在其中注册通知区通知。当达到1时,你的应用会在后台启动。然后,重新注册,以保持该位置在中心。
From Apple's Documentation:
从苹果的文档:
Apps that support background execution may be relaunched by the system to handle incoming events. If an app is terminated for any reason other than the user force quitting it, the system launches the app when one of the following events happens:
支持后台执行的应用程序可以由系统重新启动来处理传入的事件。如果一个应用程序由于除用户强制退出之外的任何原因被终止,系统会在以下事件发生时启动该应用程序:
For location apps:
定位程序:
The system receives a location update that meets the app’s configured criteria for delivery.
系统接收一个位置更新,满足应用程序配置的交付标准。
The device entered or exited a registered region. (Regions can be geographic regions or iBeacon regions.)
设备进入或退出注册区域。(区域可以是地理区域,也可以是iBeacon区域。)
#2
3
In case you need location updates frequently but still want to save battery life, try this -
如果你需要经常更新位置,但仍然想节省电池寿命,试试这个-
- Add necessary permissions for using location in background.
- 为在后台使用位置添加必要的权限。
- Start the updates using
startUpdatingLocation
- 使用startUpdatingLocation启动更新
- Start deferring updates when in background using
allowDeferredLocationUpdatesUntilTraveled:timeout:
Specify time and distance filters in this method. - 在后台使用allowdeferredlocationupdatesuntiltraveling:timeout: timeout:在此方法中指定时间和距离过滤器。
- Make sure you read the docs here and specify all the properties correctly to get it working.
- 确保您阅读了这里的文档,并正确地指定了所有属性以使其工作。
#3
2
The only solution is to use background mode with CoreLocation. If your app requires iOS 8, the system will relaunch your app, even if force quit by the user. This is documented in Apple Docs:
唯一的解决方案是使用带有CoreLocation的后台模式。如果你的应用需要ios8,系统会重新启动你的应用程序,即使用户退出。这是在苹果文档中记录的:
In most cases, the system does not relaunch apps after they are force quit by the user. One exception is location apps, which in iOS 8 and later are relaunched after being force quit by the user. In other cases, though, the user must launch the app explicitly or reboot the device before the app can be launched automatically into the background by the system.
在大多数情况下,系统不会在用户强制退出后重新启动应用程序。一个例外是位置应用程序,在ios8和之后,它会在用户强制退出后重新启动。不过,在其他情况下,用户必须显式地启动应用程序或重启设备,然后系统才能自动将应用程序启动到后台。
#4
1
in iOS 8 they are new steps that we need to pay attention to in order make make the location fetching work. The first step is to add either one or two keys into the project’s .plist depending on the main functionality of the app. The two keys are NSLocationWhenInUseUsageDescription and NSLocationAlwaysUsageDescription, you will then need to add a String that explains to the user why does the app needs to access his location, something among the lines of “This app uses location in the background/foreground because of A, B and C”. Each of these Strings has a corresponding authorization method that needs to be called, WhenInUse or Alway (i.e. Background).
在iOS 8中,它们是我们需要注意的新步骤,以使位置获取工作。第一步是将一个或两个键添加到项目的.plist取决于应用程序的主要功能。这两个键是NSLocationWhenInUseUsageDescription NSLocationAlwaysUsageDescription,然后,您将需要添加一个字符串,向用户解释为什么这个应用程序需要访问他的位置,线的东西”这个程序在后台使用位置/前景是因为,B和C”。每一个字符串都有一个相应的授权方法,需要在使用或总是(例如,后台)时调用该方法。
- (void)startStandardUpdates
{
// Create the location manager if this object does not
// already have one.
if (nil == locationManager)
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
// Set a movement threshold for new events.
locationManager.distanceFilter = 500; // meters
[locationManager startUpdatingLocation];
}
#5
0
From my current experience, you have 3 options to make this work. The first one is to have a silent notification which requires an effort done by the backend. The silent notification is some sort of a push notification but it notifies your app without an alert. The second solution is background fetches. Background fetches don't require a backend solution but sadly you can't control the intervals needed to instantiate it. The third one is having a scheduled local notifications with no messages.
根据我目前的经验,你有3个选择。第一个是要有一个无声的通知,这需要后台的努力。无声通知是某种推送通知,但它在没有警告的情况下通知你的应用程序。第二个解决方案是后台取回。后台取回不需要后端解决方案,但遗憾的是您不能控制实例化它所需的间隔。第三个是有一个计划好的本地通知,没有消息。
#6
0
From the docs
从文档
Tracking the User’s Location
跟踪用户的位置
There are several ways to track the user’s location in the background, most of which do not actually require your app to run continuously in the background:
有几种方法可以跟踪用户在后台的位置,其中大部分实际上并不要求应用程序在后台连续运行:
The significant-change location service (Recommended) Foreground-only location services Background location services
后台执行
And for the details - Getting the User’s Location
以及细节——获取用户的位置