ios - Swift 4 - 更新隐私设置 CLLocationManager 后刷新 View

标签 ios swift cllocationmanager

我正在创建一个基于位置的应用程序,当用户拒绝位置访问时,会弹出一个警告 View ,要求用户转到设置页面并更新设置以允许位置访问。如果用户允许访问然后按回键,则 View 不会使用允许位置更新的更新设置进行刷新。

是否有一个功能可以在用户从设置页面返回到应用程序后运行,该页面将刷新并开始更新位置。

我感谢任何建议或帮助。谢谢。

用户看到提醒: alert image

用户点击返回应用程序后,页面需要刷新以更新 map 上的位置: enter image description here

func alertForNoLocationAccess(){

    let alert = UIAlertController(title: "Allow Location Access", message: "Cannot add location data and map coordinates to your entries without access. Press settings to update or cancel to deny access.", preferredStyle: .alert)

    alert.addAction(UIAlertAction(title: "Settings", style: .default, handler: { action in

        if let url = URL(string:UIApplication.openSettingsURLString) {
            if UIApplication.shared.canOpenURL(url) {
                if #available(iOS 10.0, *) {
                    UIApplication.shared.open(url, options: [:], completionHandler: nil)
                } else {
                    UIApplication.shared.openURL(url)
                }
            }
        }

    }))

    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

    self.present(alert, animated: true)
}

最佳答案

ViewController/AppDelegate 注册到 NotificationCenter 以在用户从 Settings 打开应用程序时收到通知。

NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)

在用户打开应用程序时,再次向 CLLocationManager 请求位置更新。如果用户已经接受了请求,那么 CLLocationManager 将开始更新用户的位置而不会中断。

@objc func willEnterForeground() {

    //Register for
    registerForLocationUpdates()
}

func registerForLocationUpdates() {

    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.requestWhenInUseAuthorization()

    if CLLocationManager.locationServicesEnabled() {

        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        locationManager.startUpdatingLocation()
    }
}

关于ios - Swift 4 - 更新隐私设置 CLLocationManager 后刷新 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53240978/

相关文章:

ios - 单击后退按钮时,iCarousel 显示在上一页中

ios - 导航 Controller 委托(delegate)方法没有得到调用

ios - "Failure to deallocate CLLocationManager on the same runloop as its creation may result in a crash"

iOS - 设备来自其蜂窝网络的位置

ios - UIButton 不调用 ImageView 中的处理函数

ios - 处理到 App Store 后下架

ios - 如何强制 MapView 覆盖 ViewController 上的所有空间?

swift - 隐藏内容在 Swift 的 UIStackView 中不调整大小

Swift 函数 vs lazy var vs 计算属性——区别?

ios - 我不会使用位置管理器方法在标签中获得完美地址