如何在位置访问权限中按下允许按钮后执行操作?

时间:2018-03-19 06:09:10

标签: ios swift core-location

enter image description here

我正在制作一个使用GPS坐标的应用程序,在实施之前,我们必须向用户请求许可,如上图所示。

如果用户点击"允许"我想做在那个警报,然后activateGPSToSearchCoordinate()是trigerred,但如果不允许'被挖掘然后我不想做任何事情。

这是我目前的代码,但它无法正常工作

class LocationManager: NSObject {
    let manager = CLLocationManager()
    var didGetLocation: ((Coordinate?) -> Void)?

    override init() {
        super.init()

        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.requestLocation()
    }

    func getPermission() -> CLAuthorizationStatus {
        // to ask permission to the user by showing an alert (the alert message is available on info.plist)

        if CLLocationManager.authorizationStatus() == .notDetermined {
            manager.requestWhenInUseAuthorization()
            return .notDetermined
        } else if CLLocationManager.authorizationStatus() == .denied {
            return .denied
        } else if CLLocationManager.authorizationStatus() == .authorizedWhenInUse  {
            return .authorizedWhenInUse
        } else {
            return .notDetermined
        }

    }
}

我将在下面的视图控制器方法中使用该类,尤其是getPermission()

func getCoordinate() {


        let coordinateAuthorizationStatus = locationManager.getPermission()

        if coordinateAuthorizationStatus == .authorizedWhenInUse {
            activateGPSToSearchCoordinate()
        } else if coordinateAuthorizationStatus == .denied {
            showAlertSetting()
        }

    }

此刻,如果第一次触发该权限...... 要么用户点按“允许”#39;或者不允许' CLAuthorizationStatus始终为.notDetermined

所以永远不会触发activateGPSToSearchCoordinate()

所以我需要在“允许'之后”激活activateGPSToSearchCoordinate()。在那个警报被按下

如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

阅读有关CLLocationManagerDelegate的更多信息,有成功和失败的委托方法。

public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    GlobalObjects.shared.latitude  = locations[0].coordinate.latitude
    GlobalObjects.shared.longtitude = locations[0].coordinate.longitude
    GlobalObjects.shared.locationOBJ = locations[0]
    print(GlobalObjects.shared.latitude, GlobalObjects.shared.longtitude)
}

public func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
}

答案 1 :(得分:0)

CLLocationManagerDelegate怎么样?你试过这个吗?

func locationManager(CLLocationManager, didUpdateLocations: [CLLocation])
//Tells the delegate that new location data is available.


func locationManager(CLLocationManager, didFailWithError: Error)
//Tells the delegate that the location manager was unable to retrieve a location value.


func locationManager(CLLocationManager, didFinishDeferredUpdatesWithError: Error?)
//Tells the delegate that updates will no longer be deferred.


func locationManager(CLLocationManager, didUpdateTo: CLLocation, from: CLLocation)
//Tells the delegate that a new location value is available.

在您呼叫CLLocationManagerDelegate的类/视图控制器上实施func getCoordinate()

class TestViewController: CLLocationManagerDelegate {

    override func viewDidLoad() {
       super.viewDidLoad()
       locationManager.delegate = self
    }


}

public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    // get location coordinate
}

public func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
   // handle error, if any kind of error occurs
}

以下是一个很好的教程示例,可以帮助您实现CLLocationManagerDelegate