Limitation of RegionMonitoring for an iOS App

252 Views Asked by At

I am developing an app where I need to update location of device to server approximately accurate, I can afford a difference of 500 | 700 meters in the data.

I am successfully able to achieve it in a background and foreground mode. My main concern is location update when app is terminated, for that I did implemented significant location changes on app terminate but it notify the device for location changes in a random manner and highly inaccurate or there is a huge latency in the location update sometimes I receive location updates in 2-3 Kms and sometime not even in distance of 7 kms.

So one of my team member suggested me to implement region monitoring on app terminate, now I have implemented region monitoring but it is also not working properly and I have read somewhere there can only be maximum of 20 regions that can be monitored from an app.

Here is the below sample code I tried:

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)
    }
  }

}

How can I update location even if the application is terminated? In my above code is anything wrong?

1

There are 1 best solutions below

1
UIResponder On

Enable background refresh. You can keep updating location inside your app.

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