macOS 应用程序上的 CLLocationManager 不询问位置

jwk*_*knz 5 permissions macos cllocationmanager swift

我正在尝试在 macOS 应用程序上设置位置服务,但目前我无法收到请求提示。

我只有这个代码,viewDidLoad但什么也没有出现。我是/我的印象.startUpdatingLocation()是,它应该要求权限,对吗?

目前我只是想获取我自己的位置 - 是的,它在设置中启用以询问它们。

override func viewDidLoad() {
    super.viewDidLoad()

    let locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.startUpdatingLocation()
    let status = CLLocationManager.authorizationStatus()
    if status == .restricted || status == .denied {

        print("Location Denied")

        return
    }
    else if status == .notDetermined {

        print("Show ask for location")

        return
    }
    else if status == .authorized {
        print("This should work?")

        return
    }
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    currentLocation = locations[locations.count - 1]
    print("\(currentLocation)")
}
Run Code Online (Sandbox Code Playgroud)

我还需要要求什么吗?

Ank*_*ash 5

此错误消息表示应用程序尚未获得获取用户位置的权限。要解决此问题,您需要确保应用程序已正确“沙盒化”并且该位置已启用。您可以通过以下步骤在 Xcode 中执行此操作:

在项目导航器中,单击应用程序的目标。这应该会显示一个带有“常规”、“功能”、“资源标签”等选项卡的视图。

单击“功能”选项卡。这将为您提供一个项目列表,例如“应用程序组”、“应用程序沙盒”等。每个项目都有一个“开/关”按钮。

打开“App Sandbox”项,然后按左侧的“>”按钮以显示沙箱内容。

在“应用数据”部分,选择“位置”。

现在,下次运行应用程序时,它会要求用户允许它使用当前位置。假设他们的回答是肯定的,您应该不再看到错误,而是应该看到地图上突出显示的当前位置。


mrw*_*eet 1

我知道这是一个老问题,但我自己也有这个问题,你所要做的就是运行locationManager.startUpdatingLocation()。它会在开始之前询问。