Ios 在XCode 9模拟器中未调用locationManager didEnterRegion

Ios 在XCode 9模拟器中未调用locationManager didEnterRegion,ios,swift,xcode,ios-simulator,cllocationmanager,Ios,Swift,Xcode,Ios Simulator,Cllocationmanager,我有一个做地理围栏的应用程序。当我在模拟器(Xcode 8.3.3和Xcode 9)中运行它时,一切似乎都正常工作,只是从来没有调用我的CLLocationManager didEnterRegion 当我在iPhone上运行应用程序时,无论是在现实世界中(进入一个区域),还是通过位置模拟在Xcode中运行,它都被称为“刚刚好” 知道为什么会这样吗 我发现的一个不同之处是,模拟器在使用时只支持监视位置,因此我必须进行设置,以便在plist文件中同时拥有两个权限字符串,但除此之外,我感到困惑 由于

我有一个做地理围栏的应用程序。当我在模拟器(Xcode 8.3.3和Xcode 9)中运行它时,一切似乎都正常工作,只是从来没有调用我的CLLocationManager didEnterRegion

当我在iPhone上运行应用程序时,无论是在现实世界中(进入一个区域),还是通过位置模拟在Xcode中运行,它都被称为“刚刚好”

知道为什么会这样吗

我发现的一个不同之处是,模拟器在使用时只支持监视位置,因此我必须进行设置,以便在plist文件中同时拥有两个权限字符串,但除此之外,我感到困惑

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

  • 在我的应用程序方案中,我选中了“允许位置模拟”,并为我监视的位置添加了一些.gpx文件。我有一个默认位置集

  • 我的位置经理代表在我启动时被调用。我在模拟机上得到授权,在电话上得到授权

  • 位置更改时将调用locationManager(:didUpdateLocations:)

  • 调用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))")
            }
    }
    
    它是有效的。所以我的区域被监控,我的位置是我认为应该在的地方

  • 最后,没有调用我的locationManager代表的监控DIDFAILFOR和DIDFAILVERROR。并不是说他们从来没有这样做过——他们在开发过程中有过,但现在没有

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


    我做错了什么?

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

    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))")
            }
    }
    
    在iOS11中,所有应用程序必须支持WhenInUse授权,如果 始终支持授权。通过此更改,位置服务的使用 描述码已更改。为应用程序获取始终提示 显示他们必须同时具有NSLocationAlways和WhenUse Usage Description和 NSLocationWhenUsageDescription在其应用程序的Info.plist中

    因此,如果您正在调用:
    locationManager.requestAlwaysAuthorization()
    ,对于iOS 11,您需要同时拥有
    NSLocationAlways和WhenUsageDescription
    NSLocationWhenUsageDescription
    。如果你想让你的应用程序在iOS 11之前继续工作,你还需要保留
    NSLocationAlwaysUsageDescription
    ,这样你就有3个键了

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

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

    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))")
            }
    }
    
    var-permission:permission=SimulatorPlatform.isSimulator。使用时的位置:。位置始终


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

    对于iOS 11,请在您的信息中使用以下命令。plist:

      <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
      <string>Need Location</string>
    
      <key>NSLocationAlwaysUsageDescription</key>
      <string>Need Location</string>
    
      <key>NSLocationWhenInUseUsageDescription</key>
      <string>Access For Location</string>
    
    n位置始终和何时使用说明
    需要定位
    N位置始终使用说明
    需要定位
    NSLocationWhenUse用途说明
    位置访问