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

时间:2019-02-17 03:56:13

标签: swift swift4.2

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

“与名为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中启用了i aslo的隐私检查

而不是在控制台中获取坐标值,而是收到上述错误消息。在Mac上启用了定位服务。代码有什么问题?

1 个答案:

答案 0 :(得分:0)

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

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

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