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

Posted

技术标签:

【中文标题】在位置访问权限中按下允许按钮后如何执行操作?【英文标题】:how to execute an action after allow button is pressed in the location access permission? 【发布时间】:2018-03-19 06:09:09 【问题描述】:

我正在制作一个使用 GPS 坐标的应用程序,在实施之前,我们必须像上图一样征求用户的许可。

如果用户在该警报中点击“允许”,那么我想要触发 activateGPSToSearchCoordinate(),但如果点击“不允许”,那么我不想做任何事情。

这是我目前的代码,它不能正常工作

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

    

目前,如果该权限是第一次触发... 用户点击“允许”或“不允许”,CLAuthorizationStatus 将始终为 .notDetermined

所以 activateGPSToSearchCoordinate() 永远不会被触发。

所以我需要在按下该警报的“允许”后激活activateGPSToSearchCoordinate()

如何解决这个问题?

【问题讨论】:

【参考方案1】:

阅读更多关于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) 

【讨论】:

【参考方案2】:

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.

在您调用func getCoordinate() 的类/视图控制器上实现CLLocationManagerDelegate

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

https://developer.apple.com/documentation/corelocation/cllocationmanager https://www.raywenderlich.com/160517/mapkit-tutorial-getting-started https://www.appcoda.com/tag/mapkit/ http://www.techotopia.com/index.php/A_Swift_Example_ios_8_Location_Application

【讨论】:

以上是关于在位置访问权限中按下允许按钮后如何执行操作?的主要内容,如果未能解决你的问题,请参考以下文章

在首先按下不允许后如何授予对 react-native-camera 的访问权限?

我如何在标题栏中按下按钮并在 vala 的窗口中执行操作

在Phonegap / iOS中按下主页按钮时访问localStorage

在swift3中按下按钮时如何以编程方式执行segue或呈现视图

用户允许访问后如何使用浏览器地理位置

当用户在swift中按下注销按钮时如何停止更新位置