【问题标题】:locationManager didEnterRegion not called in XCode 9 SimulatorXCode 9 Simulator 中未调用 locationManager didEnterRegion
【发布时间】:2017-06-23 13:15:14
【问题描述】:

我有一个执行地理围栏的应用程序。当我在模拟器(Xcode 8.3.3 和 Xcode 9)中运行它时,一切似乎都可以正常工作,但我的 CLLocationManager didEnterRegion 从未被调用。

当我在我的 iPhone 上运行应用程序时,它被调用得很好,无论是在现实世界中(进入一个区域)还是在 Xcode 中通过位置模拟。

知道为什么会这样吗?

我发现的一个区别是模拟器只支持在使用时监控位置,所以我必须进行设置,所以我的 plist 文件中有两个权限字符串,但除此之外我很难过。

由于我没有提供代码(它太复杂并且分布在我的应用程序中),让我注意模拟器中的工作:

  1. 在我的应用程序方案中,我选中了“允许位置模拟”,并为我正在监控的位置添加了一些 .gpx 文件。我设置了默认位置。

  2. 当我启动时,我的位置管理器委托被调用。我在模拟器中获得 .authorizedWhenInUse,在手机上获得 .authorizedAlways。

  3. locationManager(:didUpdateLocations:) 在位置改变时被调用。

  4. 调用 didUpdateLocations 时,我执行以下操作:

    for r in manager.monitoredRegions { 
        if let cr = r as? CLCircularRegion {
            if cr.contains(location.coordinate) {
                log.debug("Am in the region!")
            } else {
                let crLoc = CLLocation(latitude: cr.center.latitude,
                                      longitude: cr.center.longitude)
                log.debug("distance is: \(location.distance(from: crLoc))")
            }
    }
    

    它有效。所以我的地区正在被监控,我的位置是我认为应该在的地方。

  5. 最后,我的 locationManager 委托的 monitoringDidFailFor 和 didFailWithError 没有被调用。并不是说他们从来没有 - 他们在开发期间有过,但现在没有。

所以我很难过。同样,它在手机上运行良好,而不是在模拟器中运行良好。

我做错了什么?

【问题讨论】:

    标签: ios swift xcode ios-simulator cllocationmanager


    【解决方案1】:

    好的,我发现了问题。首先,需要对 Xcode 9/iOS 11 进行更改。我已向 Apple 提交了一个错误并收到以下信息:

    在 iOS11 中,所有应用程序必须支持 WhenInUse 授权,如果它们 支持始终授权。通过此更改,位置服务的使用 描述键已更改。让应用程序获得始终提示 表明他们必须同时拥有 NSLocationAlwaysAndWhenInUseUsageDescription 和 NSLocationWhenInUseUsageDescription 在他们应用的 Info.plist 中。

    因此,如果您调用的是:locationManager.requestAlwaysAuthorization(),对于 iOS 11,您需要同时拥有 NSLocationAlwaysAndWhenInUseUsageDescriptionNSLocationWhenInUseUsageDescription。如果您希望您的应用继续在 iOS 11 之前的版本上运行,您还需要保留 NSLocationAlwaysUsageDescription,这样您将拥有 3 个键。

    我通过 Kuhncj 引用的 Ray Wenderlich 教程证实了这一点。照原样,它不适用于 Xcode 9,但适用于 Xcode 8。

    我的问题是我的错误。虽然我有 3 个正确的密钥,但在查看我请求权限的位置时,我有以下几点:

    var permission : Permission = SimulatorPlatform.isSimulator ? .locationWhenInUse : .locationAlways

    所以我基本上是为模拟器和设备运行不同的代码。更改后,我的应用确实被调用到达受监控的区域。

    【讨论】:

      【解决方案2】:

      对于 iOS 11,在 info.plist 中使用以下内容:

        <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
        <string>Need Location</string>
      
        <key>NSLocationAlwaysUsageDescription</key>
        <string>Need Location</string>
      
        <key>NSLocationWhenInUseUsageDescription</key>
        <string>Access For Location</string>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-12
        • 2016-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-21
        相关资源
        最近更新 更多