swift - iOS swift 3 : Location Privacy Denied

标签 swift location core-location privacy

如果应用的位置隐私访问被拒绝怎么办?

大家好,我正在编写一个简单的应用程序,它在使用时使用位置。

设计模式

  1. 首先,当您启动应用程序时,它会检查是否已设置权限。

    • 如果没有,它会显示一个请求许可的警报。
    • 如果是并获得批准,它将继续执行其工作。
    • 如果是但被拒绝,它会显示一条警报,要求通过指向设置的按钮授予访问权限。
      • 应该可以从设置返回到应用程序。
  2. 应用程序发挥作用。

  3. 如果用户在应用程序仍处于打开状态时更改隐私设置,则应通知该应用程序,并重复第 1 步。

到目前为止的代码

主 Controller

let manager = LocationManager()

override func viewDidLoad() {
        super.viewDidLoad()
        // ...

        if manager.getPermission() == false {
          //show alert
          showAcessDeniedAlert()
        }
        manager.onLocationFix = { 
          //This is a function used for a closure
        }

    }
}
func showAcessDeniedAlert() {
    let alertController = UIAlertController(title: "Location Accees Requested",
                                                message: "The location permission was not authorized. Please enable it in Settings to continue.",
                                                preferredStyle: .alert)

    let settingsAction = UIAlertAction(title: "Settings", style: .default) { (alertAction) in

        // THIS IS WHERE THE MAGIC HAPPENS!!!!
        if let appSettings = URL(string: UIApplicationOpenSettingsURLString) {
            UIApplication.shared.open(appSettings as URL)
        }
    }
    alertController.addAction(settingsAction)

    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
    alertController.addAction(cancelAction)

    present(alertController, animated: true, completion: nil)
}

位置管理器

import CoreLocation

extension Coordinate {
    init(location: CLLocation) {
        latitude = location.coordinate.latitude
        longitude = location.coordinate.longitude
    }
}

final class LocationManager: NSObject, CLLocationManagerDelegate {
    let manager = CLLocationManager()

    var onLocationFix: ((Coordinate) -> Void)?

    override init() {
        super.init()
        manager.delegate = self
        manager.requestLocation()
    }

  func getPermission() -> Bool {
      switch CLLocationManager.authorizationStatus() {
      case .authorizedAlways:
            return true
      case .authorizedWhenInUse:
            return true
      case .denied:
            return false
      case .restricted:
            return false
      case .notDetermined:
            manager.requestWhenInUseAuthorization()
            return getPermission()
      }
  }



    //MARK: CLLocationManagerDelegate
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        if status == .authorizedWhenInUse {
            manager.requestLocation()
        }
    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print(error.localizedDescription)
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let location = locations.first else { return }

        let coordinate = Coordinate(location: location)
        if let onLocationFix = onLocationFix {
            onLocationFix(coordinate)
        }

    }
}

我该怎么做?

  1. 如果隐私被拒绝,我如何显示 AlertController?

    使用此设置我遇到此错误:Warning: Attempt to present <UIAlertController: 0x145ae7ee0> on <xxx.xxController: 0x143e04720> whose view is not in the window hierarchy! .

  2. 如何编写指向“设置”的设置按钮?

  3. 我如何编写代码:“从设置页面,我可以返回应用程序”?

最佳答案

  1. viewDidLoad 在第一次访问 self.view 属性后被调用。这意味着在将此 self.view 添加到窗口层次结构之后,首先调用 viewDidLoad。将检查代码移动到 viewDidAppear(_:) 函数,并确保呈现您的 View Controller 。
  2. 打开设置应用的代码似乎没问题,但不要忘记检查它在哪个系统版本中可用。
  3. 您的应用在后台状态下无法以某种方式与其他应用进行交互。

关于swift - iOS swift 3 : Location Privacy Denied,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40150638/

相关文章:

php - Swift Poststring 没有得到我的值

ios - 如何在表格 View 和导航栏之间添加 View ?

android - 应用程序销毁时停止服务

ios - 应用程序在后台时的信标行为

ios - 将 Double 转换为 CLLocationDegrees [SWIFT]

ios - 每个国家的城市列表及其位置信息

ios - UIImageView:如何显示缩放图像?

ios - Swift UIPanGestureRecognizer仅从右到左

swift - Format Double 为负货币,或从 Double 中删除 "-"符号

android - map Activity 上的自定义按钮