【问题标题】:Limitation of RegionMonitoring for an iOS AppiOS 应用的 RegionMonitoring 限制
【发布时间】:2017-01-26 19:02:58
【问题描述】:

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

}

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

【问题讨论】:

    标签: swift core-location clregion


    【解决方案1】:

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

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

    【讨论】:

    • 我需要在应用程序终止时更新位置。如果您可以在此处提供有关代码和描述的帮助,那将非常有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-18
    • 1970-01-01
    • 2013-02-18
    • 2015-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多