【问题标题】:Swift 4 - Refresh View after updating privacy settings CLLocationManagerSwift 4 - 更新隐私设置 CLLocationManager 后刷新视图
【发布时间】:2018-11-10 16:27:46
【问题描述】:

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

用户从设置页面返回应用程序后,是否可以运行刷新并开始更新位置的功能。

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

用户看到警报:

用户点击返回应用后,页面需要刷新以更新地图上的位置:

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)
}

【问题讨论】:

    标签: ios swift cllocationmanager


    【解决方案1】:

    注册ViewController/AppDelegateNotificationCenter 以通知用户何时从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()
        }
    }
    

    【讨论】:

    • 这是修复它的正确方法。我已经实现了它并且它工作得很好。向 viewDidLoad 和您列出的函数添加了通知侦听器。谢谢
    • 太好了,这是正确的,谢谢
    【解决方案2】:

    查看 Apple 的文档,你会发现:

    "当您请求授权时,或当您的应用程序的授权时 状态变化,使用 locationManager(_:didChangeAuthorization:) 委托对象的方法来处理更改。清单 3 显示 启用或禁用功能的该方法的实现 基于应用当前的授权级别。”

    显然,如果用户决定关闭您获取位置数据的权限,也会触发此委托方法。

    如果您要显示此提醒,您还应该使用此通知关闭提醒,以防用户在未使用提醒中的按钮的情况下手动转到“设置”。

    【讨论】:

    • 感谢您的评论,我已经实现了委托方法,问题是当应用程序进入前台并且用户从设置视图重新进入我的 viewController 时,没有采取任何操作。 CLLocation 委托方法不处理这个问题。
    【解决方案3】:

    实现CLLocationManagerDelegate的Delegate方法

    // MARK: - CLLocationManagerDelegate
        extension LocationTracker: CLLocationManagerDelegate {
    
            func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
                print(locations)
                guard let location = locations.first else { return }
                lastLocation = location
                print(LocationTracker.shared.lastLocation)
                print("location = \(location.coordinate.latitude) \(location.coordinate.longitude)")
                locateMeCallback?(location)
            }
    
            func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
                print(error.localizedDescription)
            }
    
            func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
                enableLocationServices()
            }
    
        }
    

    /// enableLocationService 是这样的

    func enableMyAlwaysFeatures() {
            locationManager.allowsBackgroundLocationUpdates = true
            locationManager.pausesLocationUpdatesAutomatically = true
            locationManager.startUpdatingLocation()
            locationManager.delegate = self
        }
    

    【讨论】:

      猜你喜欢
      • 2015-05-22
      • 2019-02-01
      • 1970-01-01
      • 2021-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-07
      • 2015-09-11
      相关资源
      最近更新 更多