在XCode 9 Simulator中未调用locationManager didEnterRegion [英] locationManager didEnterRegion not called in XCode 9 Simulator

查看:65
本文介绍了在XCode 9 Simulator中未调用locationManager didEnterRegion的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个做地理围栏的应用程序.当我在Simulator(Xcode 8.3.3和Xcode 9)中运行它时,一切似乎都可以正常工作,唯一的例外是从未调用过CLLocationManager didEnterRegion.

I have an app that does geofencing. When I run it in the Simulator (Xcode 8.3.3 and Xcode 9), everything seems to work with the exception that my CLLocationManager didEnterRegion is never called.

当我在iPhone上运行应用程序时,无论是在现实世界中(输入区域)还是在Xcode中通过位置模拟,它都很好.

It is called just fine when I run the app on my iPhone, either out in the real world (entering a region) or in Xcode via location simulation.

有人知道为什么会这样吗?

Any idea why this would be?

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

One difference I've discovered is that the simulator only supports monitoring location when in use, so I had to set things up so I had had both permission strings in my plist file, but other than that I'm stumped.

由于我没有提供代码(它太复杂了并且无法在我的应用程序中分发),所以让我记下模拟器中正在运行的内容:

Since I'm not providing code (it's way too complex and distributed in my app), let me note what is working in the simulator:

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

  1. In my app's scheme, I have 'Allow Location Simulation' checked and I have a have a few .gpx files added for locations I'm monitoring. I have a default location set.

启动时,我的位置管理器委托正在被调用.我在模拟器中获得了.authorizedWhenInUse,并且在电话中获得了.authorizedAlways.

My location manager delegate is being called when I start up. I get .authorizedWhenInUse in the Simulator and .authorizedAlways on the phone.

locationManager(:didUpdateLocations:).

locationManager(:didUpdateLocations:) is being called when the location changes.

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

When didUpdateLocations is called, I do the following:

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))")
        }
}

,并且有效.因此,我的区域正在受到监控,我的位置也应该在该位置.

and it works. So my regions are being monitored and my location is where I think it should be.

最后,不会调用我的locationManager委托的monitoringDidFailFor和didFailWithError.并不是说他们从未去过-在开发过程中曾经经历过,但现在不是.

Finally, my locationManager delegate's monitoringDidFailFor and didFailWithError are NOT being called. Not that they've never been - they have during development, but not now.

所以我很困惑.同样,它可以在手机上正常运行,而不能在模拟器上运行.

So I'm stumped. Again, it works fine on the phone and not in the simulator.

我在做什么错了?

推荐答案

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

Ok, I found the problem. First, there is a change needed for Xcode 9/iOS 11. I had filed a bug with Apple and received the following:

在iOS11中,如果所有应用程序都必须支持WhenInUse授权, 支持始终授权.进行此更改后,定位服务的使用情况 说明键已更改.为了使应用程序获得始终"提示 显示它们必须同时具有NSLocationAlwaysAndWhenInUseUsageDescription和 他们的应用的Info.plist中的NSLocationWhenInUseUsageDescription.

In iOS11 all applications must support WhenInUse authorization if they support Always authorization. With this change the Location Services usage description keys have changed. For an application to get an Always prompt to show they must have both NSLocationAlwaysAndWhenInUseUsageDescription and NSLocationWhenInUseUsageDescription in their App’s Info.plist.

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

So if you are calling: locationManager.requestAlwaysAuthorization(), for iOS 11, you need to have both NSLocationAlwaysAndWhenInUseUsageDescription and NSLocationWhenInUseUsageDescription. If you want your app to keep working on pre-iOS 11 you need to also keep the NSLocationAlwaysUsageDescription, so you'll have 3 keys.

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

I confirmed this with Ray Wenderlich tutorial that Kuhncj's referenced. As is, it doesn't work with Xcode 9, but does with Xcode 8.

我的问题是我的错误.虽然我有3个正确的密钥,但是在查看我在哪里请求许可时,我有以下几点:

My problem was an error in my part. While I had the 3 correct keys, but in reviewing where I asked for permissions, I had the following:

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

所以我基本上为模拟器和设备运行了不同的代码.更改之后,我的应用程序确实到达了一个受监视的区域.

So I was basically running different code for the simulator vs the device. After changing that, my app did get called arriving at a monitored region.

这篇关于在XCode 9 Simulator中未调用locationManager didEnterRegion的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆