gpt4 book ai didi

swift - 如何获取用户位置?

转载 作者:可可西里 更新时间:2023-11-01 01:17:57 25 4
gpt4 key购买 nike

我正在尝试使用以下代码获取用户的当前位置,但它不起作用。我已将 NSLocationWhenInUseUsageDescription 键和 NSLocationAlwaysUsageDescription 键添加到我的 Info.plist 文件中。下面是代码

var locationManager = CLLocationManager();

override func viewDidLoad() {
super.viewDidLoad()
startReceivingLocationChanges();

}
func startReceivingLocationChanges() {
let authorizationStatus = CLLocationManager.authorizationStatus()
if authorizationStatus != .authorizedAlways {
// User has not authorized access to location information.
print("not authorized");
return
}

if !CLLocationManager.locationServicesEnabled() {
print("not enabled");
return
}

locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
locationManager.distanceFilter = 100.0 // In meters.

locationManager.startUpdatingLocation()
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let lastLocation = locations.last!
print(lastLocation)
}

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error);
}

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
print("inside didChangeAuthorization ");
}

我已经通读了苹果文档,上面是苹果建议的代码。我在这里错过了什么?非常感谢任何形式的帮助。谢谢

编辑由于某种原因 requestAlwaysAuthorization() 不可用。看下面的截图 enter image description here

最佳答案

这里的问题是,在 10.15 之前的 macOS 上,人们不会像在 iOS 上那样显式调用以请求位置访问。当您调用 startUpdatingLocation() 时,会自动显示用户权限提示。

在上面的代码中,执行永远不会到达该调用,因为您的函数 startReceivingLocationChanges 总是在检查当前状态(最有可能是“状态尚未确定”)的第一条语句中返回。因此它永远不会到达该函数中的 startUpdatingLocation() 调用,因此永远不会提示用户允许位置报告。

在 macOS 10.15 中,requestAlwaysAuthorization() 可用,但如果您只需要在使用应用时使用位置信息,则似乎不是必需的。

此外,在 macOS 上,.authorized 似乎优于 .authorizedAlways(记录为同义词),尽管添加了 requestAlwaysAuthorization() 10.15 中的功能,他们可能会更改此功能(尽管文档尚未更新以表明在回答此问题时发生了这种情况)。

如果您没有调用 requestAlwayAuthorization(),那么似乎只需要 NSLocationWhenInUseUsageDescription info.plist 键。

此外,还需要在 macOS 应用程序项目的“Signing & Capabilities”下的“Hardened Runtime”中设置“Location”复选框。这是我正在测试的 macOS 10.14.6 上的 Xcode 11.2.1 所必需的。较旧的设置或未采用强化运行时(现在是默认设置)的设置可能必须在项目build设置中的不同位置进行设置。

这是 NSViewController 子类的源代码,它在 macOS 10.14.6 上的 Xcode 11.2.1 中成功检查位置管理器和当前位置:

import Cocoa
import CoreLocation

class ViewController: NSViewController, CLLocationManagerDelegate {

let locationManager = CLLocationManager()

override func viewDidLoad() {
super.viewDidLoad()

locationManager.delegate = self
print("starting location update requests")
locationManager.startUpdatingLocation()
}


func locationManager(_ manager: CLLocationManager,
didChangeAuthorization status: CLAuthorizationStatus) {
print("location manager auth status changed to:" )
switch status {
case .restricted:
print("status restricted")
case .denied:
print("status denied")

case .authorized:
print("status authorized")
let location = locationManager.location
print("location: \(String(describing: location))")

case .notDetermined:
print("status not yet determined")

default:
print("unknown state: \(status)")
}
}

func locationManager(_ manager: CLLocationManager,
didFailWithError error: Error) {
print( "location manager failed with error \(error)" )
}
}

这适用于 macOS 如果我在第一次启动应用程序时对“启用位置服务”提示说是

控制台输出(稍微混淆):

location manager auth status changed to: status not yet determined location manager auth status changed to: status authorized location: Optional(<+4X.48,-12X.62632228> +/- 65.00m (speed -1.00 mps / course -1.00) @ 11/15/19, 10:24:30 AM Pacific Standard Time)

制作此示例的步骤:

  1. 打开 Xcode 并创建一个新的 macOS 项目
  2. 编辑项目模板提供的 ViewController 以匹配上述代码
  3. NSLocationWhenInUseUsageDescription 键添加到 info.plist
  4. 在项目设置中应用目标的“签名和功能”部分,选中“强化运行时”下的“位置”复选框。

关于swift - 如何获取用户位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45554220/

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