0
我正在開發一個應用程序,我需要定期更新用戶的位置,但是當應用程序暫停時我無法獲取位置更新或在後臺運行。我還啓用了背景位置更新功能。如何使用swift即使在應用程序在後臺運行時使用swift獲取位置更新
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]
}
希望這個答案會幫助你http://stackoverflow.com/questions/5323634/ios-application-executing-tasks-in-background – iMuzahid
我覺得這個環節有助於您 http://stackoverflow.com/questions/28503990/ios-running-background-task-for-update-user-location-using-swift – Ram
@Prakhar兄弟你必須看看這個https://github.com/ voyage11 /位置 –