【问题标题】:macos CLLocationManager Promtmacos CLLocationManager 提示
【发布时间】:2019-01-15 09:54:34
【问题描述】:

我想在从服务器收到推送通知时发送位置详细信息。但是 macOS 应用程序在启动时不会询问位置权限。在 info.plist 中添加了所有隐私项。它在调用 locationmanager.startUpdatingLocation() 时请求许可。并且没有再次询问我是否取消它。代码如下。

class AppDelegate: NSObject, NSApplicationDelegate, CLLocationManagerDelegate {
  let locationManager = CLLocationManager()
  func applicationDidFinishLaunching(_ aNotification: Notification) {
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
  }
 func scanLocationRequest{
   locationManager.startUpdatingLocation()
   // Call this when a notification receives.
 }
 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let currentLocation = locations.last
    locationManager.stopUpdatingLocation()
    sendLocationReport(currentLocation: currentLocation!)
 }

}

【问题讨论】:

  • 您的应用程序是否已添加到系统偏好设置的隐私窗格中?问题是什么?

标签: swift macos core-location cllocationmanager


【解决方案1】:

要检查用户是否有位置访问权限或不使用以下代码:

var isPermissionAvailable: Bool {
    let status = CLLocationManager.authorizationStatus()
    switch status {
    case .authorizedAlways, .authorizedWhenInUse:
        return true
    case .denied, .restricted, .notDetermined:
        requestForLocation()
        return false
    }
}

func requestForLocation() {

    // Edit
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.startUpdatingLocation()
}

要在应用启动时显示弹出窗口,您可以在 applicationDidFinishLaunching(_ aNotification:) 中的 AppDelegate 类中使用以下代码:

if isPermissionAvailable {
    // Do your work on permission available
}

【讨论】:

    猜你喜欢
    • 2018-08-13
    • 1970-01-01
    • 2012-01-09
    • 2015-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多