【问题标题】:didEnterRegion and didExitRegion don't work in iOS8didEnterRegion 和 didExitRegion 在 iOS8 中不起作用
【发布时间】: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()

有什么想法吗?谢谢。

【问题讨论】:

    标签: swift ios8 location


    【解决方案1】:

    我已经能够通过在我的 .plist 文件中设置“NSLocationAlwaysUsageDescription”来完成这项工作。

    我不确定这是否会在应用未运行时增加电池使用量,但这让我可以在进入和退出时收到通知...

    【讨论】:

    • 那些进入和退出的方法会不会触发NSLocationWhenInUseUsageDescription??
    【解决方案2】:

    不要忘记激活通知属性:

    self.beaconRegion.notifyOnEntry=YES;
    self.beaconRegion.notifyOnExit=YES;
    self.beaconRegion.notifyEntryStateOnDisplay=YES;
    

    【讨论】:

    • 我在 locationManager.startMonitoringForRegion(monitoredRegion) 之前将它与我的 CLCircularRegion 区域一起使用,但它不起作用。 :(
    猜你喜欢
    • 2013-12-13
    • 2011-05-08
    • 2015-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-14
    相关资源
    最近更新 更多