我正在开发一个应用程序,我需要更新设备到服务器的位置大致准确,我可以负担500 |的差异700米的数据。
我成功地能够在后台和前台模式下实现它。 我的主要关注点是应用程序终止时的位置更新,因为我确实在应用程序终止时实现了重要的位置更改,但它以随机方式通知设备位置更改并且高度不准确或者有时我收到的位置更新存在巨大延迟位置更新2-3公里,有时甚至没有7公里的距离。
所以我的一个团队成员建议我在应用程序终止时实施区域监控,现在我已经实现了区域监控,但它也无法正常工作,我已经阅读过某个地方,最多只能有20个可监控的区域一个应用程序。
以下是我尝试过的示例代码:
import UIKit
import CoreLocation
import XCGLogger
//import SwiftLocation
// MARK: - AppDelegate
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate
{
var window: UIWindow?
var locationManager: CLLocationManager!
var currentLocation: CLLocationCoordinate2D?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
if launchOptions != nil
{
if (launchOptions![UIApplicationLaunchOptionsLocationKey] != nil)
{
if let location = locationManager.location?.coordinate {
startRegionMonitoring(location)
}
}
}
setupLocalNotifications()
setupLocationManager()
return true
}
func setupLocationManager()
{
if (locationManager == nil)
{
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.pausesLocationUpdatesAutomatically = false
locationManager.distanceFilter = CLLocationDistance(100.0)
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
if #available(iOS 9.0, *) {
locationManager.allowsBackgroundLocationUpdates = true
}
}
}
func applicationWillTerminate(application: UIApplication)
{
if currentLocation == nil
{
return
}
// create a region around current location and monitor enter/exit
startRegionMonitoring(currentLocation!)
}
func startRegionMonitoring(location: CLLocationCoordinate2D)
{
let region = CLCircularRegion(center: location, radius: 100.0, identifier: "manish")
region.notifyOnExit = true
region.notifyOnEntry = true
locationManager.startMonitoringForRegion(region)
}
// MARK: - Local notifications
func setupLocalNotifications()
{
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
}
func showLocalNotification(message: String)
{
let localNotification = UILocalNotification()
localNotification.alertBody = message;
localNotification.fireDate = NSDate(timeIntervalSinceNow: 1.0)
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
}
}
extension AppDelegate
{
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus)
{
if status == .AuthorizedAlways {
locationManager.startUpdatingLocation()
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
currentLocation = locations.first?.coordinate
showLocalNotification("didUpdateLocations: \(currentLocation)")
}
func locationManager(manager: CLLocationManager, didEnterRegion region: CLRegion)
{
showLocalNotification("Entered region: \(region.identifier)")
}
func locationManager(manager: CLLocationManager, didExitRegion region: CLRegion)
{
showLocalNotification("Exited region: \(region.identifier)")
if let location = manager.location?.coordinate {
startRegionMonitoring(location)
}
}
}
即使申请被终止,我如何更新位置?在我上面的代码中有什么不对吗?
答案 0 :(得分:0)
启用后台刷新。您可以继续更新应用内的位置。
http://solutionowl.com/background-app-refresh-explained-in-laymans-terms/