2016-08-04 41 views
0

我目前正在創建一個使用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

它看起來不像completionHandler被稱爲 – PeejWeej

+0

授權過程沒有完成處理程序。你將得到一個委託方法'didChangeAuthorizationStatus'的調用。可能最好的做法是在點擊簽入按鈕並請求位置訪問時設置一個標誌,然後在通過檢查代理方法中的標誌進行授權後恢復簽入過程。您還需要考慮一旦授予位置信息並開始獲取位置信息,則可能需要幾秒鐘才能獲得準確的修正 – Paulw11

回答

1

而不是添加閉包,您可以使用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!) 
} 
+0

謝謝,此工作已完成 – SwiftyJD

相關問題