如何为系统方法添加闭包?

时间:2016-08-04 20:28:44

标签: ios swift cllocationmanager

我目前正在创建一个使用CLLocationManager的应用程序,但我没有在应用程序委托中询问位置权限的提示,而是在按下“"签入"被压了。我试图创建一个允许用户在接受允许位置服务后签到的闭包。目前,在用户接受位置服务之后,检查它们的代码未被激活,因为在用户实际接受位置服务之前检查是否启用了位置服务。这是我的代码:

typealias CompletionHandler = (success:Bool) -> Void

func askLocationPermission (completionHandler: CompletionHandler) {
    self.locationsManager.requestWhenInUseAuthorization()
}

@IBAction func checkInButtonPressed(sender: AnyObject) {

    askLocationPermission { (success) in
        if CLLocationManager.locationServicesEnabled() {
            self.locationsManager.delegate = self

            if let location = self.locationsManager.location {
                self.currentUserLatitude = location.coordinate.latitude
                self.currentUserLongitude = location.coordinate.longitude

                print("This is the current latitide: \(location.coordinate.latitude)")
                print("This is the current longitude: \(location.coordinate.longitude)")

                self.checkInLocation(
                    userInfo.sharedInstance.getAccessToken(), 
                    id: userInfo.sharedInstance.getMemberID()!, 
                    latitude: self.currentUserLatitude!, 
                    radius: 0.3, 
                    longitude: self.currentUserLongitude!)
            }
        }  
    }
}

1 个答案:

答案 0 :(得分:2)

您可以只使用CLLocationManager提供的委托结构,而不是添加闭包。这是documentation for CLLocationManager(以及有关如何使用它的更大教程),这里是documentation for the delegate

首先,当按下按钮时,我们想要注册为委托,以便我们从CLLocationManager获得所有更新。这样,CLLocationManager会告诉我们授权状态何时更改以及何时获取新位置。在我们注册为代表后,我们检查授权状态。如果尚未确定,我们会要求用户许可。否则我们要么没有访问权限,要么用户已经给予了许可,我们就抓住了他们的位置!

@IBAction func checkInButtonPressed(sender: AnyObject) {

    // Set us as a delegate so the manager updates us
    self.locationsManager.delegate = self

    // Check our authorization status and request access if we need
    if CLLocationManager.authorizationStatus() ==  kCLAuthorizationStatusNotDetermined {
        // User hasn't given permission yet, ask for permission
        self.locationsManager.requestWhenInUseAuthorization()
    } else if CLLocationManager.authorizationStatus() ==  kCLAuthorizationStatusRestricted || CLLocationManager.authorizationStatus() ==  kCLAuthorizationStatusDenied {
        // Handle denied access
    } else {
        // We have access! Start listening now
        self.locationsManager.startUpdatingLocation()
    }
}

接下来,我们要处理用户尚未授予我们访问权限的方案,并向他们提供权限弹出窗口。他们可以点击是(或不),所以我们想要处理这两种情况。就像上面一样,如果他们点击是,就开始获取位置!

// Called when the user changes the authorization status- in this case
//  this will change when the user taps yes/no in the permission popup
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    if CLLocationManager.authorizationStatus() ==  kCLAuthorizationStatusRestricted || CLLocationManager.authorizationStatus() ==  kCLAuthorizationStatusDenied {
        // Handle denied access
    } else {
        // We have access! Start listening now
        self.locationsManager.startUpdatingLocation()
    }
}

最后,我们可以开始做事!只要有新位置,CLLocationManager就会调用它,所以这是办理登机手续的最佳时机!获得良好的锁定可能需要一段时间,因此您可能需要在checkInButtonPressed中提醒用户您正在处理它并将很快更新它们。

// Called every time the locationManager gets a new location
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    self.currentUserLatitude = manager.coordinate.latitude
    self.currentUserLongitude = manager.coordinate.longitude

    print("This is the current latitide: \(location.coordinate.latitude)")
    print("This is the current longitude: \(location.coordinate.longitude)")

    self.checkInLocation(
        userInfo.sharedInstance.getAccessToken(), 
        id: userInfo.sharedInstance.getMemberID()!, 
        latitude: self.currentUserLatitude!, 
        radius: 0.3, 
        longitude: self.currentUserLongitude!)
}