gpt4 book ai didi

cllocationmanager - 在 Swift 中实现 CLLocationManagerDelegate 方法

转载 作者:IT王子 更新时间:2023-10-29 05:06:29 26 4
gpt4 key购买 nike

一段时间以来,我一直在努力让它发挥作用,我来这里是想问一下——如何在 Swift 中使用 CLLocationManagerDelegate 方法?我已经把它放在类(class)的前列了:

var locationManager = CLLocationManager()

我已将以下内容放入我的 viewDidLoad 方法中:

locationManager.delegate = self
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()

我试过使用这些委托(delegate)方法但没有用:

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: AnyObject[]!) {
locationReceived = true
}

func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
locationReceived = false
}

我也试过在函数前使用@optional,但 Xcode 会抛出一个编译器错误。有什么想法吗?

最佳答案

您需要将 NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription 键添加到您的 plist 中,如果您还没有的话,它们现在是强制性的,


iOS8+ 要求将这两个字符串之一设置为使用位置。您使用哪一个取决于您打算如何询问位置。

  • 对想要使用设备位置的应用使用 NSLocationAlwaysUsageDescription,即使在应用未打开和使用时也是如此。

  • NSLocationWhenInUseUsageDescription 用于希望仅在应用打开和使用时使用设备位置的应用。

注意:当您添加字符串时,在构建和运行之前,从您的设备上删除应用程序并让它进行全新安装。似乎如果应用程序在您升级到 iOS8 之前被授权使用位置,它不会再次请求您的许可,也不会看到您设置了这些字符串。进行删除和全新安装可以解决此问题。

设置任一字符串都会在安装/首次使用时提示弹出消息:“即使您未使用该应用程序,也允许“ThisApp”访问您的位置”

这是 plist 文件的屏幕截图

enter image description here

关于cllocationmanager - 在 Swift 中实现 CLLocationManagerDelegate 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24050633/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com