CLLocationManager
The CLLocationManager class is the central point for configuring the delivery of location- and heading-related events to your app. You use an instance of this class to establish the parameters that determine when location and heading events should be delivered and to start and stop the actual delivery of those events. You can also use a location manager object to retrieve the most recent location and heading data.
CLLocationManager
类是为APP
配置位置相关事件
和方向相关事件
的核心. 使用该类的实例来配置方向事件和位置事件的参数, 这些参数决定了何时启用或停止事件传递, 事件何时被交付出去. 也可以使用该类的实例来检索最近的位置数据
和方向数据
.
Overview | 概述
A location manager object provides support for the following location-related activities:�
locationManager对象为位置相关活动所提供的支持如下:
-
Tracking large or small changes in the user’s current location with a configurable degree of accuracy.�
基于精确度的配置程度来追踪用户当前位置的大幅度或小范围的改变. -
Reporting heading changes from the onboard compass. (iOS only)�
通过板载磁力传感器来汇告方向的改变. (仅iOS可用) -
Monitoring distinct regions of interest and generating location events when the user enters or leaves those regions. �
监听确切的兴趣区域并在用户进入或离开这些区域时生成位置事件. -
Deferring the delivery of location updates while the app is in the background. (iOS only)�
当应用处于后台时, 延迟位置更新的事件递交. -
Reporting the range to nearby beacons. �
向附近的Beacons设备汇报范围
To configure and use a CLLocationManager object to deliver events:�
使用并配置CLLocationManager对象来递交事件:
-
Always request authorization to use location services and check to see whether the desired services are available as described in Requesting Permission to Use Location Services. �
使用定位服务总是需要请求权限, 并检查想要使用的服务是否可用, 在Requesting Permission to Use Location Services中描述. -
Create an instance of the CLLocationManager class and store a strong reference to it somewhere in your app.
Keeping a strong reference to the location manager object is required until all tasks involving that object are complete. Because most location manager tasks run asynchronously, storing your location manager in a local variable is insufficient.�
创建CLLocationManager类的实例并在你有应用中的某处进行强引用. �在locationManager对象所涉及的所有任务都已经完成之前, 必须对该对象保持强引用. 储存locationManager到局部变量上是不足够的, 因为大部分locationManager的任务都会异步处理. -
Assign a custom object to the delegate property. This object must conform to the CLLocationManagerDelegate protocol.�
给delegate属性赋值一个自定义对象, 该对象必须遵守CLLocationManagerDelegate协议. -
Configure any additional properties relevant to the desired service.�
配置所需服务相关的所有附加属性 -
Call the appropriate start method to begin the delivery of events.�
调用对应的开始方法来启动事件传递 -
All location- and heading-related updates are delivered to the associated delegate object, which is a custom object that you provide. For information about the delegate methods you use to receive events, see CLLocationManagerDelegate Protocol Reference. �
所有位置相关和事件相关的更新都会递交到关联的代理对象, 即你所提供的自定义对象. 更多用于接收事件的代理方法相关信息, 查看<a>CLLocationManagerDelegate Protocol Reference</a>
Requesting Permission to Use Location Services | 请求使用定位服务的权限
The use of location services requires user authorization. In addition, some location services require the presence of specific hardware on the given device. For example, heading information is available only on devices that contain a hardware compass. Prior to using location services, your app must request authorization from the user to use those services and it must check the availability of the target services. A typical sequence for using location services is as follows:�
定位服务的使用要求得到用户授权. 除此之外, 一些定位服务还要求指定设备具备特定的硬件. 比如, 只有在设备包含了指南针硬件时方向信息才可用. 在使用定位服务之前, 你的应用必须向用户请求使用这些服务的授权, 并且必须检查目标服务是否可用. 使用定位服务的普遍过程如下:
-
Call the authorizationStatus class method to get the current authorization status for your app.
If the authorization status is kCLAuthorizationStatusRestricted or kCLAuthorizationStatusDenied, your app is not permitted to use location services and you should abort your attempt to use them. �
调用类方法authorizationStatus
为你的应用获取当前的授权状态. �如果授权状态是kCLAuthorizationStatusRestricted
或者kCLAuthorizationStatusDenied
, 说明你的应用是不被允许使用定位服务的, 应该中断使用它们的企图. -
Create your CLLocationManager object and assign a delegate to it.�
创建CLLocationManager对象并为它赋值代理对象 -
Store a strong reference to your location manager somewhere in your app. �
在应用的某处为locationManager进行强引用 -
In iOS, if the authorization status was kCLAuthorizationStatusNotDetermined, call the requestWhenInUseAuthorization or requestAlwaysAuthorization method to request the appropriate type of authorization from the user. �
在iOS, 如果授权状态是kCLAuthorizationStatusNotDetermined
, 调用requestWhenInUseAuthorization
或者requestAlwaysAuthorization
方法向用户请求对应类型的授权 -
Depending on the services you need, call one or more of the following methods: �
根据你所需要的服务, 调用如下方法(判断服务是否可用):-
If you use the standard location service, call the locationServicesEnabled method.�
如果使用标准定位服务, 调用locationServicesEnabled
方法. -
If you use the significant location-change service, call the significantLocationChangeMonitoringAvailable method.�
如果使用 Significatn location-change(有效位置改变) 服务, 调用significantLocationChangeMonitoringAvailable
方法 -
If you use heading information, call the headingAvailable method.�
如果使用方向信息, 调用headingAvailable
方法 -
If you monitor geographic or beacon regions, call the isMonitoringAvailableForClass: method.�
如果监听地理区域或Beacon区域, 调用isMonitoringAvailableForClass:
方法 -
If you perform ranging on Bluetooth beacons, call the isRangingAvailable�
如果执行蓝牙Beacon的距离检测, 调用isRangingAvailable
-
Important
Always request authorization at the point where you actually plan to use location services to perform a task. Requesting authorization may display an alert to the user. If it is not clear to the user that your app is using location services for a useful purpose, the user may deny your request to use those services.
当你确实准备使用定位服务来执行任务时, 总是要先请求授权. 请求授权可能会向用户展示弹窗. 如果不能清晰的向用户描述你的应用使用定位服务的目的, 用户可能会拒绝你使用这些服务的请求.
It is safe to start location services before the authorization status of your app is determined. Although you can start location services, those services do not deliver any data until the authorization status changes to kCLAuthorizationStatusAuthorizedAlways or kCLAuthorizationStatusAuthorizedWhenInUse. To be notified when the authorization status changes, implement the locationManager:didChangeAuthorizationStatus: method in your location manager delegate. �
在你的应用授权状态做出决定之前, 开启定位服务是安全的. 尽管你可以开始定位服务, 但直到授权状态变成kCLAuthorizationStatusAuthorizedAlways
或者kCLAuthorizationStatusAuthorizedWhenInUse
前, 这些服务都不会递交任何数据, 想要察觉授权状态发生的变化, 在locationManager的emphasized text代理中实现locationManager:didChangeAuthorizationStatus:
方法
For the services you use, configure any properties associated with that service accurately. Core Location manages power aggressively by turning off hardware when it is not needed. For example, setting the desired accuracy for location events to one kilometer gives the location manager the flexibility to turn off GPS hardware and rely solely on the WiFi or cell radios, which can lead to significant power savings.�
对于你所使用的服务, 要精确的配置与该服务关联的所有属性. Core Location会积极的管理电源, 当硬件不再需要使用时将其关闭. 比如, 将定位事件的desiredAccuracy(期望的精确度)设置成一千米能灵活的关闭GPS硬件, 仅仅依靠Wifi或者无线, 这可以有效的节省电量.
Getting the User’s Current Location | 获取用户的当前位置
There are two options for configuring location-related services:�
配置定位相关服务时有两种选择:
-
Use the standard location service, which allows you to specify the desired accuracy of the location data and receive updates as the location changes. �
使用标准定位服务, 可以指定定位数据的期望精确度, 以及接收位置改变时的更新. -
Use the significant location change service, which provides a more limited set of tracking options but offers significant power savings over the standard location services. In iOS, this service can also launch your app as needed to deliver location updates. �
使用重要位置变更服务, 提供了相对有限的追踪选项, 但相较于标准定位服务能有效节省电量. 在iOS, 该服务还能在需要时运行你的应用来执行位置更新.
The standard location service is most appropriate for apps that deliver location-related information directly to the user but it may be used by other types of apps too. To start the service, configure the desiredAccuracy and distanceFilter properties of the location manager and then call the requestLocation, startUpdatingLocation, or allowDeferredLocationUpdatesUntilTraveled:timeout: method. Never specify an accuracy value greater than what you need. Core Location uses the accuracy value you specify to manage power better. A higher degree of accuracy requires more precise hardware like GPS, which consumes more power.
标准定位服务是最适合会直接递交位置相关信息给用户类型的应用, 其它类型应用也同样适用. 要开启该服务, 配置定位服务的desiredAccuracy
和distanceFilter
属性, 之后调用requestLocation
, startUpdatingLocation
, allowDefferredLocationUpdatesUntilTraveled:timeout:
方法. 永远不要将精确度指定为比你所需的值更高. Core Location会使用你所指定的精确度值对电量进行更好的管理. 更高级别的精确度要求更精确的硬件. 如GPS, 会消耗更多的电量.
The significant location change service is better suited for apps that want to get the user’s initial location and then only want to know when that location changes. This service requires the presence of cellular hardware and delivers events less frequently than the standard location services. To start the significant location-change service, call the startMonitoringSignificantLocationChanges method.
重要位置变更服务更适合要获取用户初始位置并仅需要知道位置何时发生改变的应用. 该服务需要蜂窝网络硬件, 相较标准定位服务会更低频率的递交事件. 要开启重要位置变更服务, 调用startMonitoringSignificantLocationChanges
方法.
Regardless of which location service you use, location data is reported to your app via the location manager’s associated delegate object. Because it can take several seconds to return an initial location, the location manager typically delivers the previously cached location data immediately and then delivers more up-to-date location data as it becomes available. Therefore it is always a good idea to check the timestamp of any location object before taking any actions. If both location services are enabled simultaneously, they deliver events using the same set of delegate methods.
不管你使用哪种定位服务, 应用的定位数据都是通过LocationManager关联的代理对象来汇报的. 由于返回初始位置需要消耗数秒的时间, 通常LocationManager会立即递交之前缓存的定位数据, 之后在最新的位置数据可用时再次递交. 因此在追踪任何事件前检查所有位置数据对象的timestamp往往是个好想法. 如果两种定位服务同时启用, 它们会使用同样的代理方法来递交事件.
Use the allowDeferredLocationUpdatesUntilTraveled:timeout: method to defer the delivery of location data when your app is in the background. It is recommended that you use this feature in situations where your app could process the data later without any problems. For example, an app that tracks the user’s location on a hiking trail could defer updates until the user hikes a certain distance and then process the points all at once. Deferring updates helps save power by allowing your app to remain asleep for longer periods of time.
当你的应用处于后台时, 使用allowDeferredLocationUpdatesUntilTraveled:timeout:
方法可以延迟位置数据的递交. 对于可以稍后再处理且不会有问题的数据, 应用在处理时建议使用该特征. 比如, 追踪用户徒步施行的位置可以延迟更新, 直到用户达到一定距离后再一次性处理所有的点. 通过允许应用保持较长时间周期的睡眠, 延迟更新可以帮助节省电量.
Using Regions to Monitor Boundary Crossings | 使用区域来监听范围穿越
You can use the region-monitoring service to be notified when the user crosses a region-based boundary. Region monitoring applies both to geographical regions (represented by the CLCircularRegion class) and to beacon regions (represented by the CLBeaconRegion class). You use region monitoring to detect boundary crossings of the specified region and you use those boundary crossings to perform related tasks. For example, upon approaching a dry cleaners, an app could notify the user to pick up any clothes that had been dropped off and are now ready.
可以使用区域监听服务, 在用户穿越区域的范围时得到通知. 区域监听即可以用在地理区域(由CLCircularRegion
类表示)也可以用在Beacon信号区域(由CLBeaconRegion
类表示). 使用区域监听来检测指定区域的范围跨越, 并使用这些范围的跨越(进入或离开区域)来执行相关任务. 比如, 在接近干洗店时, 应用可以通知用户去取回之前放下而现在已经弄好的衣服.
To start region monitoring, configure the appropriate region object and pass it to the startMonitoringForRegion: method of your location manager. In iOS, registered regions persist between launches of your app. If a region boundary crossing occurs while your iOS app is not running, the system automatically wakes it up (or relaunches it) in the background so that it can process the event. In this case, the options dictionary passed to the application:didFinishLaunchingWithOptions: method of your app delegate contains the key UIApplicationLaunchOptionsLocationKey to indicate that your app was launched because of a location-related event. During the relaunch process, you must recreate your location manager object and assign a delegate capable of handling region-related events. After you do that, the system delivers the region notification for which your app was launched. All of the regions you configured previously are made available in the monitoredRegions property of any location manager objects you create.
要开始区域监听, 配置合适的区域对象并将其传递给LocationManager的startMonitoringForRegion:
方法. 在iOS, 应用运行期间已注册区域会持续存在. 如果区域范围的跨越发生时应用并没有处于运行状态, 系统会自动从后台唤醒它(或者重新运行它)来处理事件. 在这种情况下, 传递到app delegate的application:didFinishLaunchingWithOptions:
方法中的options字典包含UIApplicationLaunchOptionsLocationKey键, 该key决定你的应用是否因为定位相关事件而启动. 在重启处理期间, 你必须重新创建locationManager对象并赋值代理以便具备处理区域相关事件的能力. 在这之后, 系统会向运行着的应用递交区域的通知. 之前你所创建的任何locationManager对象的monitoredRegions属性中所配置的所有区域都会生效.
In OS X, region monitoring works only while the app is running (either in the foreground or background) and the user’s system is awake. The system does not launch apps to deliver region-related notifications. Similarly, if the user puts the computer to sleep, the system does not deliver region monitoring notifications to your app. If the user wakes up the computer inside a monitored region, the system does deliver region notifications to your app if it is running. However, if the computer enters and exits the region before being woken up, no notification is delivered.
在OS X, 区域监听仅在应用正在运行(不管前台运行还是后台运行)且用户的系统是唤醒时才会生效. 系统并不会去运行应用来传递区域相关的通知. 同样的, 如果用户将电脑休眠, 系统也不会传递区域监听的通知给你的应用. 如果用户唤醒的计算机中监听了区域, 系统才会递交区域的通知给你正在运行的应用. , 如果计算机在它唤醒前进入和离开区域, 则不会有通知被传递.
The region monitoring service operates independently of any location services in use by your app, and you may use it in conjunction with any of the other services. Region monitoring is not supported on all devices. Use the regionMonitoringAvailable class method to determine if region monitoring can be used.�
区域监听服务的操作独立于应用中使用的所有定位服务, 你可以将它与任何其它服务结合使用. 并不是所有设备都支持区域监听. 使用类方法regionMonitoringAvailable
来判断区域监听是否能被使用.
Determining the Proximity to Beacons | 检测与Beacon的接近程度
You can use the beacon ranging service to determine the approximate distance to devices that support iBeacon technology. Beacon ranging involves listening for devices that are broadcasting their presence over Bluetooth LE. When you start ranging, you receive information about the identify of each beacon that is in range, including information about whether that beacon is nearby or farther away. Updates are then delivered whenever the status of a beacon changes.
支持iBeacon技术的设备可以使用Beacon测距服务来判断大概距离. Beacon测距涉及到对正在使用低功耗蓝牙广播它们存在的设备进行监听. 当你开始测距时, 会接收到在有效范围内的每个Beacon身份的相关信息, 包括Beacon是靠近还是远离的信息. 当Beacon的状态改变后会更新事件递交.
To start beacon ranging, create a CLBeaconRegion object that identifies the beacons you want and pass that object to the startRangingBeaconsInRegion: method. As the location manager detects beacons, it delivers that information to its delegate’s locationManager:didRangeBeacons:inRegion: method. In an area where many beacons are active and the user is moving, updates may be delivered frequently. To preserve battery life, do not range for beacons all the time. The recommended practice is to use the region monitoring service to detect the presence of beacons first and to start ranging only after you detect one or more beacons. And, of course, you should stop ranging as soon as you have the information you need or when you move out of range of your beacons.�
要开始Beacon测距, 创建能够标识你想要Beacon的CLBeaconRegion对象并将该对象传递给startRangingBeaconsInRegion:
方法. 当locationManager检测到检测到Beacon时, 会将信息传递给它代理的locationManager:didRangeBeacons:inRegion:
方法. 有多个有效Beacon且用户正移动时, 可能会频繁的递交更新事件. 要节省电量, 则不要一直检测Beacon. 建议的做法是先使用区域监听服务来检测Beacon的存在, 在你确定Beacons存在后再开始测距. 当然, 在你拿到需要的信息或者离开了Beacon的区域后就应该停止测距.
Configuring Heading-Related Services | 配置方向相关服务
In iOS, you can use the heading service to obtain compass-related information on devices with the appropriate hardware. Apps that implement navigation or spatial orientation features can use the compass information to determine which way a user is facing. The heading service is not supported in OS X.
在iOS, 你可以使用方向服务从设备的相关硬件上获取指南针相关的信息. 要实现导航或空间定向功能的应用可以使用指南针信息来确定用户正面对哪个方向. 方向服务并不支持OS X.
To begin the delivery of heading-related events, assign a delegate to the location manager object and call its startUpdatingHeading method. If location updates are also enabled, the location manager returns both the true heading and magnetic heading values. If location updates are not enabled, the location manager returns only the magnetic heading value.
要开始递交方向相关事件, 给locationManager对象赋值代理并调用它的startUpdatingHeading
方法. 如果位置更新也启用, locationManager即返回真北方向也返回磁北方向的值. 如果位置更新没有启用, 则locationManager仅返回磁北方向的值.
Core Location delivers heading data to your app while it is in the foreground. When your app moves to the background, event delivery stops unless your app has the location-updates background mode enabled and the standard location service is running. You can enable background modes from the Capabilities tab of your Xcode project but should do so only if you can use heading data practically while in the background.
当应用处于前台时, Core Location会向应用传递方向数据. 当应用被移到后台, 事件传递会被中止, 除非应用启用了后台位置更新模式且标准定位服务正在运行. 可以在Xcode项目的Capabilities标签中启用后台模式, 但你应该在确实需要在后台状态下使用到方向数据时才这么做.
Getting the Visited Locations | 获取访问过的位置
In iOS, the visits service provides an alternative to the significant location change service for apps that need location information about interesting places that the user visited. For example, if the user is in one location for an extended period of time, the service might generate an event when the user arrives at that location and another when the user leaves that location. The service is intended for apps that might already be using the significant location change service and want an even lower power way to do so. You would not use this service to create navigation apps or apps that rely on regular location updates.
在iOS, 为了获取用户访问过的兴趣地点的位置信息, 应用可以使用访问服务
, 它是显著位置变更服务
的替代方案. 比如, 如果在一段时间之前用户曾在某个位置呆过, 该服务能在用户到达或离开该位置时分别生成事件. 该服务用于已经使用了显著位置变更服务的应用想要得到更低的电量消耗的情况下. 你不应该使用该服务来创建导航应用或依靠定期位置更新的应用.
To begin the delivery of visit-related events, assign a delegate to the location manager object and call its startMonitoringVisits method. As the location manager generates visit events, it delivers that information to its delegate’s locationManager:didVisit: method. The event data delivered to your delegate includes only the information that occurred after you started the delivery of events. In other words, if you start the delivery of events after the user arrived at an interesting location, the event delivered by the system when the user departed that location would not reflect the actual arrival time. If the system terminates your app, this service relaunches it when new visit events are ready to be delivered.
要开始访问相关事件的递交, 赋值locationManager对象的代理并调用它的startMonitoringVisits
方法. 当locationManager生成访问事件时, 它会传递这些信息到代理的locationManager:didVisit:
方法. 传递到代理的事件数据仅包含了开始递交事件后所发生的信息. 换名话说, 如果你在用户到达了兴趣位置后开始事件传递, 则当用户离开该位置时系统递交的事件中不会反映出实际到达的时间. 如果系统终止你的应用, 当新的访问事件准备好递交时该服务会重启应用.
Using Location Services in the Background | 在后台使用定位服务
Most location services are meant to be used while your app is in the foreground but some can also run in the background. In some cases, location events can even cause your app to be relaunched to process an event. To run most location services in the background, you need to enable the location updates background mode in the Capabilities tab of your Xcode project. For services that launch your app, you need to request (and be granted) “Always” authorization from the user.
大部分定位服务理应在应用处于前台时使用, 但有时候它也能在后台运行. 在一些案例中, 位置事件甚至可以重新运行你的应用来处理该事件. 大部分的定位服务想在后台运行时, 需要启用你的Xcode项目的Capabilities标签中的location updates background mode. 对于能运行你应用的服务, 需要向用户请求Always权限(并得到授权).
-
The standard location service delivers events normally while an app is running in the foreground. When your app is in the background, this service delivers events only when the location-updates background mode is enabled for the app. This service does not relaunch iOS apps that have been terminated. �
当应用处于前台运行时, 标准定位服务正常的递交事件. 当应用进入后台, 该服务仅在应用的后台位置更新模式启用时才会递交事件. 该模式并不会重新启动已经被终止的应用. -
The significant location change service delivers events normally while an app is running in the foreground or background. For a terminated iOS app, this service relaunches the app to deliver events. Use of this service requires “Always” authorization from the user. �
当应用处于前台或后台运行时, 重要位置变更服务都会正常的递交事件. 对于被终止的应用, 该服务会重新启动应用来递交事件. 使用该服务必须向用户请求Always授权. -
The region monitoring service delivers events normally while an app is running in the foreground or background. (You can use this service for both geographic and beacon regions.) For a terminated iOS app, this service relaunches the app to deliver events. Use of this service requires “Always” authorization from the user.�
当应用处于前台或后台运行时, 区域监听服务正常递交事件. (可以为地理区域和Beacon区域使用该服务.) 对于被终止的应用, 该服务重新启动应用来递交事件. 使用该服务要求得到用户的Always授权. -
Beacon ranging delivers events normally while an app is running in the foreground. When your app is in the background, this service delivers events only when the location-updates background mode is enabled for the app and the standard location service is running. (If the beacon region’s notifyEntryStateOnDisplay property is YES, waking the device causes the app to range for beacons for a few seconds in the background.) This service does not relaunch iOS apps that have been terminated; however, you can be relaunched by monitoring beacon regions using the region monitoring service.�
在应用处于前台运行时, Beacon测距正常的递交服务. 当应用处于后台时, 该服务仅在应用启用了后台位置更新模式且标准定位服务正在运行时才会递交事件. (如果BeaconRegion的notifyEntryStateOnDisplay属性为YES, 在进入Beacon范围后数秒后会从后台唤醒设备.) 该服务并不会重新启动被终止的iOS应用; 然而, 你可以使用区域监听服务来监听Beacon区域使应用重新运行. -
The heading service delivers events normally while an iOS app is running in the foreground. When your app is in the background, this service delivers events only when the location-updates background mode is enabled for the app and the standard location service is running. This service does not relaunch iOS apps that have been terminated.�
当应用处于前台运行时, 方向服务正常的递交事件. 当应用处于后台时, 该服务仅在应用的后台位置更新模式启用并且标准定位服务正在运行时才会递交事件. 该服务并不会重新启动被终止的应用. -
The visit service delivers events normally while an iOS app is running in the foreground. When your app is in the background, this service delivers events only when the location-updates background mode is enabled for the app and the standard location service is running. For a terminated iOS app, this service relaunches the app to deliver events. Use of this service requires “Always” authorization from the user.�
当应用处于前台运行时, 访问服务正常的递交事件. 当应用处于后台时, 该服务仅在应用的后台位置更新模式启用并且标准定位服务正在运行时才会递交事件. 该服务会重新启动被终止的应用. 使用该服务要求得到用户的Always授权
Enabling the location-updates background mode ensures that an app continues to receive location events while in the background. When the app moves to the background, the system adds the location-services indicator to the status bar to let the user know that an app is using location services. The system may still terminate the app at any time to reclaim its memory or other resources.
启用了后台定位更新模式确保了应用处在后台时可以继续接收定位事件. 当应用移动到后台, 系统会添加定位服务指示器到状态栏上以便让用户知道该应用正在使用定位服务. 系统依旧有可能在任意时该终止应用, 以回收内存和其它资源.
NOTE
In iOS 8 and later, disabling the Background App Refresh setting for the current app or for all apps does not prevent the delivery of location events in the background. In previous versions of iOS, disabling Background App Refresh prevented the delivery of events in the background. �
在iOS8及之后, 禁用当前应用的后台应用刷新设置并不会阻止后台状态下定位事件的递交. 在之前版本的iOS, 禁用后台应用刷新会阻止后台状态下事件的递交.
For services that relaunch the app, the system adds the UIApplicationLaunchOptionsLocationKey key to the options dictionary passed to the app delegate at launch time. When this key is present, you should restart your app’s location services right away. The options dictionary does not include information about the location event itself. You must configure a new location manager object and delegate and start your location services again to receive any pending events. In iOS 7.1 and later, services that cause an app to be relaunched do so even after the user force-quits the app. In previous versions of iOS, force-quitting an app prevented the app from being relaunched.
对于会重新启动应用的服务, 系统会向启动时传递到App Delegate的options字典(即LaunchOptions)添加UIApplicationLaunchOptionsLocationKey
键值. 当该键值出现时, 你应该立即重启应用的定位服务. options字典并不包含定位事件本身的信息. 你必须配置新的locationManager对象和代理对象, 并再次开启定位服务来接收所有待处理的事件. 在iOS7.1及之后, 即使在用户强制退出应用能应用重启的服务也会工作. 在之前的iOS版本中, 强制退出应用会阻止应用从后台重启.
If your app does not need to do anything with location events right away, consider deferring location updates using the allowDeferredLocationUpdatesUntilTraveled:timeout: method. Deferring the delivery of location updates makes it easier for Core Location to preserve battery life while still delivering the location events your app needs.
如果你的应用并不需要立即使用定位服务做什么事情, 考虑使用allowDeferredLocationUpdatesUntilTraveled:timeout:
方法来延迟位置的更新. 延迟位置更新的递交仍然会递交你所需要的位置事件, 但Core Location能节省电量.
网友评论