2016-08-04 5 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

完了ハンドラが呼び出されていないようです – 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() 
    } 
} 

次に、ユーザーがまだ私たちにアクセス権を与えていないシナリオを処理したいと思います。許可ポップアップを提示します。彼らはyes(またはno)をタップして、両方のケースを処理したいと思う。ちょうど上記のように、彼らが「はい」をタップした場合、ちょうど場所を取得し始める!

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

関連する問題