私は現在、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!)
}
}
}
}
完了ハンドラが呼び出されていないようです – PeejWeej
承認プロセスの完了ハンドラはありません。デリゲートメソッド 'didChangeAuthorizationStatus'が呼び出されます。おそらく、あなたができることは、チェックインボタンがタップされているときにフラグを設定し、ロケーションアクセスを要求してから、デリゲートメソッドのフラグをチェックして認可を取得した後にチェックインプロセスを再開することです。また、場所のアクセスが許可され、場所の取得が開始されると、正確な修正が行われるまでに数秒かかることを考慮する必要があります – Paulw11