【发布时间】:2017-02-07 05:06:32
【问题描述】:
我正在开发一个需要定期更新用户位置的应用程序,但是当应用程序暂停或在后台运行时,我无法获取位置更新。我还在功能中启用了后台位置更新。
viewDidLoad 代码:
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.allowsBackgroundLocationUpdates = true
locationManager.requestAlwaysAuthorization()
locationManager.requestWhenInUseAuthorization()
let user=FIRAuth.auth()?.currentUser?.displayName
if(CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedAlways || CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse){
locationManager.requestLocation()
location=locationManager.location
locationManager.startUpdatingLocation()
locationManager.startMonitoringSignificantLocationChanges()
//updating to database
ref.child("\(user!)/lat").setValue(location.coordinate.latitude)
ref.child("\(user!)/long").setValue(location.coordinate.longitude)
//location=locationManager.location
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
location=locations[locations.count-1]
}
【问题讨论】:
-
希望这个答案能帮到你stackoverflow.com/questions/5323634/…
-
我认为这个链接对你有帮助stackoverflow.com/questions/28503990/…
-
@Prakhar 兄弟你必须看看这个github.com/voyage11/Location
-
我正在使用 swift 为 ios 9 及更高版本开发应用程序
-
你能告诉我你的代码吗?