如何在 ios 应用程序中跨所有视图控制器跟踪用户位置?
How to track user location across all view controllers in an ios app?
我正在构建一个应用程序,该应用程序在应用程序打开时跟踪用户位置并使用 geofire 查询它。我想从用户可能打开的任何视图控制器中更新位置。我想知道如何做到这一点。
目前,我在主视图控制器中设置了 geofire 和位置跟踪。为了让位置跟踪在所有视图控制器中工作,我是否必须对每个单独的视图控制器进行编码以跟踪位置,或者我是否可以将代码放入例如应用程序委托文件中?
您可以将代码放在AppDelegate 中。也许在访问服务器时使用计时器,以便优化性能
参考这个答案:
Location Manager update frequency, iphone
可能这段代码会有所帮助,但您必须在所有视图控制器中使用它
var LocationManager = CLLocationManager()
LocationManager.requestWhenInUseAuthorization()
if( CLLocationManager.authorizationStatus() == .authorizedWhenInUse ||
CLLocationManager.authorizationStatus() == .authorizedAlways){
print("currentLocation = \(LocationManager.location)")
}
使用HSLocationManager在活动和非活动状态下进行无限位置跟踪。
Location manager that allows getting background location updates every
n seconds with desired location accuracy.
Advantage:
OS will never kill our app if the location manager is currently
running.
Give periodically location update when it required(range is between 2 -
170 seconds (limited by max allowed background task time))
Customizable location accuracy and time period.
Low memory consumption(Singleton class)
- Less battery draining.
放在应用程序中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)")
}
我正在构建一个应用程序,该应用程序在应用程序打开时跟踪用户位置并使用 geofire 查询它。我想从用户可能打开的任何视图控制器中更新位置。我想知道如何做到这一点。
目前,我在主视图控制器中设置了 geofire 和位置跟踪。为了让位置跟踪在所有视图控制器中工作,我是否必须对每个单独的视图控制器进行编码以跟踪位置,或者我是否可以将代码放入例如应用程序委托文件中?
您可以将代码放在AppDelegate 中。也许在访问服务器时使用计时器,以便优化性能
参考这个答案: Location Manager update frequency, iphone
可能这段代码会有所帮助,但您必须在所有视图控制器中使用它
var LocationManager = CLLocationManager()
LocationManager.requestWhenInUseAuthorization()
if( CLLocationManager.authorizationStatus() == .authorizedWhenInUse ||
CLLocationManager.authorizationStatus() == .authorizedAlways){
print("currentLocation = \(LocationManager.location)")
}
使用HSLocationManager在活动和非活动状态下进行无限位置跟踪。
Location manager that allows getting background location updates every n seconds with desired location accuracy.
Advantage:
OS will never kill our app if the location manager is currently running.
Give periodically location update when it required(range is between 2 - 170 seconds (limited by max allowed background task time))
Customizable location accuracy and time period.
Low memory consumption(Singleton class)
- Less battery draining.
放在应用程序中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)")
}