我遇到了大约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)
}
还有很多要给你看的。但是不幸的是,这太多了……
答案 0 :(得分:2)
请从项目功能中启用“后台模式”,然后启用“位置更新”。启用此功能后,唯一在后台获取更新(不处于终止状态)的配置是将“ allowsBackgroundLocationUpdates”设置为true(您已经完成)。
此处,仅当您希望在应用程序被用户杀死时获取位置时,才需要进行重大位置更改。位置的重大更改将在后台启动应用程序并读取设备的位置。有关在后台获取位置的更多信息,请遵循:
有关应用程序处于终止状态的重要位置更改,请点击以下链接。这是在目标C中完成的,但也可以轻松快速地完成。
希望这会有所帮助。