didEnterRegion、didExitRegion 未被调用

didEnterRegion, didExitRegion not being called

我一直在试验区域监控,以便在用户位于设定区域内时显示警报或本地通知。作为第一步,我添加了一个打印行以查看它是否适用于调试区域。但是,在打印其他行时,我没有得到 didEnterRegion 和 didExitRegion 的任何信息。

我正在将位置模拟为给定区域的 in/outside,但我没有运气。如果有人可以查看下面的代码并了解我错过了什么,那就太好了。谢谢。

    import UIKit
    import CoreLocation

    class ViewController: UIViewController, CLLocationManagerDelegate {

    var manager = CLLocationManager?()



        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
            manager = CLLocationManager()
            let latitude: CLLocationDegrees = 48.858400
            let longitude: CLLocationDegrees = 2.294500
            let center: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
            let radius: CLLocationDistance = CLLocationDistance(100.0)
            let identifier: String = "Notre Dame"

            let currRegion = CLCircularRegion(center: center, radius: radius, identifier: identifier)

            manager?.distanceFilter = 10
            manager?.desiredAccuracy = kCLLocationAccuracyBest
            currRegion.notifyOnEntry = true
            currRegion.notifyOnExit = true

            manager?.requestWhenInUseAuthorization()


            manager?.delegate = self
            manager?.pausesLocationUpdatesAutomatically = true

            manager?.startMonitoringForRegion(currRegion)
            manager?.startUpdatingLocation()    


        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }

        func locationManager(manager: CLLocationManager, didStartMonitoringForRegion region: CLRegion) {
            print("The monitored regions are: \(manager.monitoredRegions)")
        }

        func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
            let locValue:CLLocationCoordinate2D = manager.location!.coordinate
            print("locations = \(locValue.latitude) \(locValue.longitude)")
        }

        func locationManager(manager: CLLocationManager, didEnterRegion region: CLRegion)  {
            NSLog("Entered")
        }

        func locationManager(manager: CLLocationManager, didExitRegion region: CLRegion) {
            NSLog("Exited")
        }


    }

您可以通过更改

使其工作

manager?.requestWhenInUseAuthorization()

manager?.requestAlwaysAuthorization()

然后将此密钥添加到您的 info.plist 文件中 NSLocationAlwaysUsageDescription 值为 "This is for testing purpose" 或您想要的任何文本,这将显示给请求使用 location

的用户