【问题标题】:iOS Swift 3: Location Privacy DeniediOS Swift 3:位置隐私被拒绝
【发布时间】:2017-03-02 05:31:02
【问题描述】:

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

晚上好,我正在编写一个简单的应用程序,该应用程序在使用时使用位置

设计模式

  1. 在一切之前,当你启动应用程序时,它会检查是否已经设置了权限。

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

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

到目前为止的代码

主控制器

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. 如何编码:“从设置页面,我可以返回应用程序”?

【问题讨论】:

    标签: swift location core-location privacy


    【解决方案1】:
    1. viewDidLoad 在首次访问 self.view 属性后被调用。这意味着在此self.view 添加到窗口层次结构之后,首先调用viewDidLoad。将检查代码移至 viewDidAppear(_:) 函数,并确保显示您的视图控制器。
    2. 打开设置应用的代码似乎没问题,但不要忘记检查它是从哪个系统版本可用的。
    3. 您的应用在后台状态下无法与其他应用进行交互。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-28
      • 1970-01-01
      • 1970-01-01
      • 2011-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多