在macos / swift中未激活位置服务提示/授权

问题描述 投票:0回答:1

无法获取swift 4中macos app的当前位置的纬度和经度值,而是在控制台中收到错误消息

“名为com.apple.locationd.desktop.synchronous的服务连接无效。” UserInfo = {NSDebugDescription =名为com.apple.locationd.desktop.synchronous的服务连接无效。}

注册计时器已过期,但客户端仍在注册!

class ViewController: NSViewController, CLLocationManagerDelegate 
{

let locationManager = CLLocationManager()
}


 override func viewDidLoad(){

       super.viewDidLoad()
      locationManager.delegate = self
       locationManager.desiredAccuracy = kCLLocationAccuracyBest
       locationManager.startUpdatingLocation()
        print (locationManager.location?.coordinate.latitude ?? 0);
        print (locationManager.location?.coordinate.longitude ?? 0);
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
         print (locationManager.location?.coordinate.latitude ?? 0);
         print (locationManager.location?.coordinate.longitude ?? 0);
     }

我还在info.plist中启用了隐私检查

而不是在控制台中获取坐标值我得到如上所述的错误消息。在mac上启用了位置服务。代码中有什么问题?

swift swift4.2
1个回答
0
投票

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

  1. 在项目导航器中,单击应用程序的目标。这应该会显示一个视图,其中包含“常规”,“功能”,“资源标签”等选项卡。
  2. 单击“功能”选项卡。这将为您提供一个项目列表,如“应用程序组”,“应用程序沙箱”等。每个项目都有一个“开/关”按钮。
  3. 打开“App Sandbox”项目,然后按左侧的“>”按钮显示沙盒内容。
  4. 在“应用程序数据”部分中,选择“位置”。

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

© www.soinside.com 2019 - 2024. All rights reserved.