macos CLLocationManager Promt

时间:2019-01-15 09:54:35

标签: swift macos core-location cllocationmanager

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

ng serve

1 个答案:

答案 0 :(得分:0)

要检查用户是否具有位置信息访问权限,请使用以下代码:

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
}