简体   繁体   English

应用程序终止/暂停时的后台用户位置

[英]Background user location when app is terminated/suspended

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. 我一直试图找出如何在应用程序终止时获取用户的位置,就像应用程序Moves一样。 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: 根据Apple的说法:

Apps can expect a notification as soon as the device moves 500 meters or more from its previous notification. 只要设备从之前的通知移动500米或更长时间,应用就会收到通知。 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. 如果设备能够从网络检索数据,则位置管理器更有可能及时发送通知。

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. 在Moves应用程序中,即使应用程序终止,它也能够非常准确地检索用户的位置,而不会有太多的电池排水。 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. 所以我想知道像Moves这样的应用程序是如何做到这一点的。 Any help would be great! 任何帮助都会很棒! Thanks. 谢谢。

You can register for regions in 500m area and register for notification region notification in them. 您可以注册500米区域内的区域,并在其中注册通知区域通知。 When it reaches one if them, your app is launched in background. 如果它们达到一个,您的应用程序将在后台启动。 Then, re-register for new ones keeping that location at center. 然后,重新注册新的位置,将该位置保持在中心位置。

在此输入图像描述

From Apple's Documentation : 来自Apple的文档

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区域。)

In case you need location updates frequently but still want to save battery life, try this - 如果您经常需要更新位置但仍希望节省电池寿命,请尝试以下操作 -

  1. Add necessary permissions for using location in background. 添加在后台使用位置所需的权限。
  2. Start the updates using startUpdatingLocation 使用startUpdatingLocation启动更新
  3. Start deferring updates when in background using allowDeferredLocationUpdatesUntilTraveled:timeout: Specify time and distance filters in this method. 使用allowDeferredLocationUpdatesUntilTraveled:timeout:在后台开始延迟更新allowDeferredLocationUpdatesUntilTraveled:timeout:在此方法中指定时间和距离过滤器。
  4. Make sure you read the docs here and specify all the properties correctly to get it working. 请务必阅读此处的文档并正确指定所有属性以使其正常工作。

The only solution is to use background mode with CoreLocation. 唯一的解决方案是使用CoreLocation的后台模式。 If your app requires iOS 8, the system will relaunch your app, even if force quit by the user. 如果您的应用需要iOS 8,系统将重新启动您的应用,即使用户强制退出也是如此。 This is documented in Apple Docs : Apple Docs中记录了这一点:

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. 位置应用程序是一个例外,它在iOS 8及更高版本中被用户强行退出后重新启动。 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. 但是,在其他情况下,用户必须明确启动应用程序或重新启动设备,然后系统才能将应用程序自动启动到后台。

in iOS 8 they are new steps that we need to pay attention to in order make make the location fetching work. 在iOS 8中,它们是我们需要注意的新步骤,以便使位置获取工作。 The first step is to add either one or two keys into the project's .plist depending on the main functionality of the app. 第一步是根据应用程序的主要功能将一个或两个键添加到项目的.plist中。 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”. 这两个键是NSLocationWhenInUseUsageDescription和NSLocationAlwaysUsageDescription,然后您需要添加一个String,向用户解释为什么应用程序需要访问他的位置,“此应用程序使用位置在后台/前台因为A, B和C“。 Each of these Strings has a corresponding authorization method that needs to be called, WhenInUse or Alway (ie Background). 这些字符串中的每一个都有一个相应的授权方法,需要调用,即InInUse或Alway(即背景)。

- (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];
}

From my current experience, you have 3 options to make this work. 根据我目前的经验,您有3个选项可以完成这项工作。 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. 第三个是没有消息的预定本地通知。

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 

Background Execution 后台执行

And for the details - Getting the User's Location 并获取详细信息 - 获取用户的位置

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

相关问题 当应用程序被杀死/终止/挂起时,cordova获取iOS 7和8的位置更新 - cordova getting Location Updates for iOS 7 and 8 when the App is Killed/Terminated/Suspended 应用终止时后台位置跟踪不起作用 - Background location tracking not working when the app is terminated 在应用程序终止时获取用户的位置 - Getting the user's location when app is terminated 当应用程序在后台甚至被用户终止时,获取用户位置而无需在iOS上移动 - Get user location without movement on iOS when app is in background or even terminated by user 即使应用处于后台/挂起/已终止状态,是否可以使CoreData与服务器API保持同步? - Is there a way to keep CoreData in Sync with a Server API even when the App is in Background/Suspended/Terminated states? 应用程序在后台暂停以进行位置更新 - App gets suspended in background for Location Update 应用程序在后台或暂停时的位置更新 - Location updates while app is in the background or suspended 终止/暂停时重大更改位置 API 的行为? - Behaviour for significant change location API when terminated/suspended? 在后台和暂停的应用模式下更新位置 - Location update in background and suspended app modes 应用终止后,位置背景模式能否快速运行? - Does the location background mode work in swift when the app is terminated?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM