[英]Update location when user is in background - swift
我遇到了3-4個月的問題。 我已經嘗試了所有您能想到的方法來使它起作用,但是我真的不能。 現在,我正在尋找您的幫助來解決此問題。
我有一個應用程序,當您按下開始按鈕時,它應該可以找到位置。 (當您在應用程序上運行時,效果很好。)但是,一旦離開應用程序,(不終止進程)就會轉到后台。 折線的繪制效果不理想。 停頓一下。
我需要一個可以在這里為我提供幫助的人,或者與我創建一個聊天室,以便我們進行討論,然后我將發送其余的代碼。
這是其中的一部分,我認為這是最重要的。 在viewDidLoad內部
let app = UIApplication.shared
NotificationCenter.default.addObserver(self, selector: #selector(applicationWillResignActive(notification:)), name: UIApplication.willResignActiveNotification, object: app)
NotificationCenter.default.addObserver(self, selector: #selector(didBecomeActive(notification:)), name: UIApplication.didBecomeActiveNotification, object: app)
-
@objc func applicationWillResignActive(notification: NSNotification)
{
start = CFAbsoluteTimeGetCurrent()
print("Background entered")
startReceivingSignificantLocationChanges()
}
@objc func didBecomeActive(notification: NSNotification)
{
let elapsed = CFAbsoluteTimeGetCurrent() - start
counter = counter + Int(elapsed)
print("Returned to application")
locationManager.stopMonitoringSignificantLocationChanges()
}
<在開始按鈕內。
//Checking userpermission to allow map and current location
if (CLLocationManager.locationServicesEnabled())
{
locationManager.requestAlwaysAuthorization()
locationManager.requestWhenInUseAuthorization()
self.locationManager.allowsBackgroundLocationUpdates = true
self.locationManager.showsBackgroundLocationIndicator = true
//Retrieve current position
if let userLocation = locationManager.location?.coordinate
{
//Zooming in to current position
let viewRegion = MKCoordinateRegion(center: userLocation, latitudinalMeters: 200, longitudinalMeters: 200)
mapView.setRegion(viewRegion, animated: false)
//Creating a start annotation
if locations.isEmpty
{
let annotation = MKPointAnnotation()
annotation.title = "Start"
annotation.coordinate = userLocation
mapView.addAnnotation(annotation)
}
self.locations.append(userLocation)
print(self.locations, "First")
//Starts the walk-timer, with interval: 1 second
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateCounter), userInfo: nil, repeats: true)
//Sending to update
update()
}
}
<背景工作者
func startReceivingSignificantLocationChanges()
{
let authorizationStatus = CLLocationManager.authorizationStatus()
if authorizationStatus != .authorizedAlways
{
return
}
if !CLLocationManager.significantLocationChangeMonitoringAvailable()
{
// The service is not available.
return
}
else
{
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
locationManager.distanceFilter = 100.0 //100.0 meters
locationManager.activityType = .fitness
locationManager.allowsBackgroundLocationUpdates = true
locationManager.delegate = self
locationManager.startMonitoringSignificantLocationChanges()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations
locations: [CLLocation])
{
let lastLocation = locations.last!
self.locations.append(lastLocation.coordinate)
print("Locations retrieved from background: ", self.locations)
}
我還有很多要給你看的。 但是不幸的是,這太多了……
請從項目的功能啟用后台模式,然后啟用“位置更新”。 啟用此功能后,唯一在后台獲取更新(不處於終止狀態)的配置是將“ allowsBackgroundLocationUpdates”設置為true(您已經完成)。
在這里,僅當您希望在用戶殺死應用程序時獲取位置時,才需要進行重大位置更改。 位置的重大更改將在后台啟動應用程序並讀取設備的位置。 有關在后台獲取位置的更多信息,請遵循:
要在應用程序處於終止狀態時進行重要的位置更改,請點擊以下鏈接。 這是在目標C中完成的,但也可以輕松快速地完成。
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.