【发布时间】:2015-02-10 12:08:44
【问题描述】:
我正在使用核心位置来跟踪一个区域,以检测用户是否进入或退出该区域。
不幸的是,我的代码在 iOS7 中有效,但在 iOS8 中无效。
这是我正在使用的代码:
func setMonitoredRegion() {
var startLocation: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: +52.53753000, longitude: +13.35971000)
var monitoredRegion = CLCircularRegion(center: startLocation, radius: 100, identifier: "Region Test")
locationManager.startMonitoringForRegion(monitoredRegion)
}
在委托方法didStartMonitoringForRegion中开始跟踪区域:
func locationManager(manager: CLLocationManager!, didStartMonitoringForRegion region: CLRegion!) {
println("Starting monitoring \(region.identifier)")
}
但在 iOS8 中没有调用方法 didEnterRegion 和 didExitRegion:
func locationManager(manager: CLLocationManager!, didEnterRegion region: CLRegion!) {
println("Entered Region \(region.identifier)")
self.showAlertViewWithTitle("Enter Region", message: "The user has entered in monitored region").show()
}
func locationManager(manager: CLLocationManager!, didExitRegion region: CLRegion!) {
println("Exited Region \(region.identifier)")
self.showAlertViewWithTitle("Exit Region", message: "The user has left monitored region").show()
}
另外,我没有收到任何错误:
func locationManager(manager: CLLocationManager!, monitoringDidFailForRegion region: CLRegion!, withError error: NSError!) {
println("Error:" + error.localizedDescription)
}
或者:
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
println("Error:" + error.localizedDescription)
}
在 iOS8 中,我在 info.plist 文件中包含以下键:
NSLocationWhenInUseUsageDescription
在代码中我获得了用户授权:
locationManager.requestWhenInUseAuthorization()
有什么想法吗?谢谢。
【问题讨论】: