我正在使用“核心位置”来跟踪区域,以便检测用户是否输入o退出该区域。
不幸的是,我的代码只能在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()
任何的想法?谢谢。
通过在我的.plist文件中设置“ NSLocationAlwaysUsageDescription ”,我可以使它正常工作。
我不确定是否在应用未运行时是否会增加电池使用量,但是这会在进入和退出时通知我...
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句