简体   繁体   English

Xcode 7.1 iOS 9.1 CLLocationManager并不总是有效吗?

[英]Xcode 7.1 iOS 9.1 CLLocationManager not always working?

When implementing a CLLocationManager I came across a problem. 实现CLLocationManager时遇到了一个问题。 Sometimes, when launching the app in the iPhone Simulator, it just doesn't fetch the current locations. 有时,在iPhone模拟器中启动应用程序时,它只是无法获取当前位置。 But when I restart the app or it then suddenly works after 1-3 restarts. 但是,当我重新启动应用程序或它然后突然工作1-3重启后。 Restarting Xcode works too... Here's my code: 重启Xcode也有效......这是我的代码:

private var lat = 0.0;
private var long = 0.0;

private var didFindLocation = false

let locationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()

    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.requestWhenInUseAuthorization()
    self.locationManager.startUpdatingLocation()

}

func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    if (status == .AuthorizedAlways) || (status == .AuthorizedWhenInUse) {
        didFindLocation = false
        locationManager.startUpdatingLocation()
    }
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location = locations.last! as CLLocation
    self.lat = location.coordinate.latitude
    self.long = location.coordinate.longitude

    if !didFindLocation {
        print("\(self.lat) - \(self.long)")
        didFindLocation = true
        locationManager.stopUpdatingLocation()
    }
}

As you can see, I've created a bool didFindLocation that allows me to fetch the location only once. 如您所见,我创建了一个bool didFindLocation ,它允许我只获取一次该位置。 I've put some breakpoints to see what's going on, and when the location doesn't get fetched, func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) doesn't even get called. 我已经设置了一些断点来查看发生了什么,并且当没有获取位置时, func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])甚至都没有被调用。

Thanks 谢谢

This can happen on a simulator, but do add this method so that you get an error message of why it´s not working even though it´s on a simulator 这可能发生在模拟器上,但是添加此方法以便您收到错误消息,说明为什么它不能正常工作,即使它在模拟器上

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
     print(error.localizedDescription)
}

iOS Simulator do not contains sensors like accelerometer, gyrometer etc, so we should not expect to work them on simulators. iOS模拟器不包含加速度计,陀螺仪等传感器,因此我们不应期望在模拟器上使用它们。 Besides this, for fetching a location, perhaps, it is using your system's internet. 除此之外,对于获取位置,也许它正在使用您系统的互联网。

So for a fair result it is advisable to use a real device for such cases. 因此,为了获得公平的结果,建议在这种情况下使用真实设备。

From coding perspective, you can check for locationServicesEnabled property of CCLocationManager to see if this service is enabled or not. 从编码角度来看,您可以检查CCLocationManager的 locationServicesEnabled属性,以查看是否启用了此服务。

When you check location in simulator then first you need to point your simulator at some location. 当您在模拟器中检查位置时,首先需要将模拟器指向某个位置。 Why? 为什么?

Because simulator is not real time device first you need to point it at some position or location. 因为模拟器首先不是实时设备,所以需要将其指向某个位置或位置。

How you can do it? 你怎么能这样做?

There are two ways to do it. 有两种方法可以做到这一点。

1) select the simulator -> Goto debug -> select location -> select apple location. 1)选择模拟器 - >转到调试 - >选择位置 - >选择苹果位置。

Your location method called 您的位置方法称为

Noto : if you select custom location from menu and enter your custom location then you need to restart your simulator (some time multiple times) Noto:如果您从菜单中选择自定义位置并输入您的自定义位置,那么您需要重新启动模拟器(多次一些时间)

在此输入图像描述

2) Goto xcode -> run your project -> in debug panel you find one location icon -> click on it -> it display location name -> select any place name 2)转到xcode - >运行你的项目 - >在调试面板中你找到一个位置图标 - >点击它 - >它显示位置名称 - >选择任何地名

Note : Not necessary it worked all the times. 注意:没有必要它一直工作。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM