为什么在 iOS 模拟器中没有调用 LocationManager,但在 phone 中调用了它?

Why is LocationManager not being called in iOS Simulator, but it's being called on the phone?

//in my viewDidLoad()
    self.locationManager.requestWhenInUseAuthorization()
    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.startUpdatingLocation()
    self.locationManager.startMonitoringSignificantLocationChanges()

    if CLLocationManager.authorizationStatus() == CLAuthorizationStatus.Denied{
        println("location not authorized")
    }


//somewhere down the file

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    println("Location") //this prints on the phone, not simulator
    self.locationManager.stopUpdatingLocation()
}

我的plist里也有"NSLocationWhenInUseUsageDescription"。

只需从这里重置您的模拟器:

希望对您有所帮助。

这是一个 iOS 8 相关的问题。您必须将 NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription 键放入您的 .plist 文件中(值可能是将在位置警报中显示的附加消息)。 iOS 8.

中需要这些键

Apple 指南中的说法:

我最终解决了自己的问题。

显然在 iOS 8 SDK 中,在开始位置更新之前需要 requestAlwaysAuthorization(用于后台定位)或 requestWhenInUseAuthorization(仅在前台时定位)调用 CLLocationManager .

Info.plist 中还需要 NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription 键,并在提示中显示一条消息。添加这些解决了我的问题。

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

目前 iOS8 beta5 中存在一个错误,它总是停用您的应用程序的地理定位服务(至少对我的应用程序而言)。

进入设置 > 隐私 > 定位服务 > 您的应用程序 > 始终。

但我不知道为什么,但即使您将其设置为“始终”,此设置也会自动停用,因此请耐心等待并经常在设置中return重新配置您的应用程序位置。

在第一次启动模拟器时,即使通过设置 from 也不会初始化位置 调试-> 位置-> 来自 ios8 的自定义位置。因此,您需要先从 debug->Location->None select none 选项,如下所示

[![enter image description here][1]][1]
[1]: http://i.stack.imgur.com/WAl14.png

and then next you should following

[![enter image description here][2]][2]

  [2]: http://i.stack.imgur.com/oCtfB.png

现在您终于应该 运行 再次在模拟器上使用您的应用程序 你的 plist 中的 NSLocationWhenInUseUsageDescription 键你应该使用下面的代码:

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

模拟器没有启用 GPS。因此,对于基于位置的事物,我们应该使用 Device。默认情况下,模拟器将始终为您提供一个位置 (0,0)。