ios - 如何在 ios 应用程序中跨所有视图控制器跟踪用户位置?
问题描述
我正在构建一个应用程序,该应用程序在应用程序打开时跟踪用户位置并使用 geofire 查询它。我想从用户可能打开的任何视图控制器保持位置更新。我想知道如何做到这一点。
目前,我在主视图控制器中设置了地理火灾和位置跟踪。为了让位置跟踪在所有视图控制器中工作,我是否必须编写每个单独的视图控制器来跟踪位置,或者我可以将代码放入例如应用程序委托文件中?
解决方案
将其放入应用程序 delegate.swift
let locationManager = CLLocationManager()
self.locationManager.requestAlwaysAuthorization()
// For use in foreground
self.locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
//Here Get Late long.
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
print("locations = \(locValue.latitude) \(locValue.longitude)")
}
推荐阅读
- grafana - 基于模板变量创建单态面板的动态仪表板
- maven - 使用 Maven 生成 JaCoCo 代码覆盖率报告
- java - 如何创建一个写入数据库内容的rowmapper类返回一个bean类
- python-2.7 - Dict Comprehension - 为什么调试器告诉我我的 dict 是一个集合?
- mongodb - findOne 与 $push 创建重复对象
- html - 从另一个组件以编程方式聚焦输入框
- python - 无法弄清楚如何计算 .wav 文件的光谱特性
- haskell - “‘节点’应用于太少的参数”即使有确切的数字
- sql - 连接表时缺少数据
- javascript - DOM 完成后运行脚本