0

我正在开发一个应用程序,我需要将设备的位置更新为大致准确的服务器,我可以承受 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)
    }
  }

}

即使应用程序终止,我如何更新位置?在我上面的代码中有什么问题吗?

4

1 回答 1

0

启用后台刷新。您可以在应用程序中不断更新位置。

http://solutionowl.com/background-app-refresh-explained-in-laymans-terms/

于 2016-09-19T07:14:10.320 回答