我一直在尝试区域监控,以便在用户位于设置区域内时显示提醒或本地通知。作为第一步,我添加了一条打印行,看它是否在调试区域工作。然而,在打印其他行的同时,我没有为didEnterRegion和didExitRegion获取任何内容。
我正在模拟给定区域内/外的位置,但我没有运气。如果有人能够查看下面的代码,看看我错过了什么,那将会很棒。谢谢。
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")
}
}
答案 0 :(得分:1)
您可以通过更改
来使其工作管理器?.requestWhenInUseAuthorization()
到
管理器?.requestAlwaysAuthorization()
然后将此密钥添加到info.plist文件中 带有值&#34的 NSLocationAlwaysUsageDescription ;这是出于测试目的"或者您想要的任何文本这是请求使用位置的用户所看到的内容