Swift 4-更新隐私设置CLLocationManager

时间:2018-11-10 16:27:46

标签: ios swift cllocationmanager

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

在用户从设置页面重新进入应用程序后,是否有可以运行的功能,该功能将刷新并开始更新位置。

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

用户警告: alert image

在用户单击返回应用程序之后,需要刷新页面以更新地图上的位置: 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)
}

3 个答案:

答案 0 :(得分:3)

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

  

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

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

如果要显示此警报,则还应使用此通知来解除警报,以防用户手动进入“设置”而不使用警报中的按钮。

答案 1 :(得分:2)

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

答案 2 :(得分:0)

CLLocationManagerDelegate的实现委托方法

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