ndPPPhz
ndPPPhz

Reputation: 325

startMonitoring doesn't work with "WhenInUseAuthorization" permission

I was working to a project but the monitoring function of a region didn't work.

So I created a quite simple project for testing this function. I found a fun situation.

The code is the following below:

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {
    @IBOutlet weak var map: MKMapView!

    let clMan = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        map.showsUserLocation = true

        clMan.requestWhenInUseAuthorization()
        clMan.delegate = self
        clMan.startUpdatingLocation()
        clMan.startMonitoring(for:CLCircularRegion(center: CLLocationCoordinate2D.init(latitude:  51.5100888, longitude: -0.134575500000008), radius: 1000, identifier: "Picca")
)

    }


    func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
        print(region)
    }
} 

The strange thing is that if I change

requestWhenInUseAuthorization() with requestAlwaysAuthorization() my code works!.

I set Piccadilly Circus for testing with the simulation of the location. Before going to London, I set another city and later London!

Upvotes: 1

Views: 893

Answers (1)

Dave Weston
Dave Weston

Reputation: 6635

From the documentation:

If the user grants “when-in-use” authorization to your app, your app can start most (but not all) location services while it is in the foreground. (Apps cannot use any services that automatically relaunch the app, such as region monitoring or the significant location change service.

More from documentation:

In iOS, registered regions persist between launches of your app. If a region boundary crossing occurs while your iOS app is not running, the system automatically wakes it up (or relaunches it) in the background so that it can process the event.

Upvotes: 1

Related Questions