Siri Intent get user location

I tried to use location manager (CLLocationManager) in a custom intent handler, so I can make some calculations based on Latitude and Longitude. But didUpdateLocations method is never called, neither didFailWithError. It is possible to use Location services in an intent used with Siri?

Replies

It is possible to get location with an intent — can you share some code? One possible way you might see this happen is if you're not considering the asynchronous nature of getting location updates, so you exit the intent handling code path before the updates arrive.

  • Thanks for the answer This is the code I have for the intent handling:

    `import Foundation import Intents import CoreLocation

    class CustomRequestHandler: NSObject, CustomIntentHandling, CLLocationManagerDelegate {

    private var locationManager: CLLocationManager? var intentCompletion: ((INObjectCollection<NSString>?, Error?) -> Void)? func resolveUnit(for intent: CustomDefinitionIntent, with completion: @escaping ([INStringResolutionResult]) -> Void) { var values = [INStringResolutionResult]() values.append(INStringResolutionResult.success(with: "Result 01")) values.append(INStringResolutionResult.success(with: "Result 02")) values.append(INStringResolutionResult.success(with: "Result 03")) completion(values) } // This is not being called func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { print("locations = \(locations)") intentCompletion?(INObjectCollection(items: results), nil) } // This is not being called func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { print("LocationManager error:\(error.localizedDescription)") } func provideUnitOptionsCollection(for intent: CustomDefinitionIntent, with completion: @escaping (INObjectCollection<NSString>?, Error?) -> Void) { self.intentCompletion = completion self.locationManager = CLLocationManager() self.locationManager?.desiredAccuracy = kCLLocationAccuracyNearestTenMeters self.locationManager?.pausesLocationUpdatesAutomatically = false self.locationManager?.delegate = self self.locationManager?.startUpdatingLocation() } func confirm(intent: CustomDefinitionIntent, completion: @escaping (CustomDefinitionIntentResponse) -> Void) { let userActivity = NSUserActivity(activityType: "someactivity.custom.com") completion(UnlockDefinitionIntentResponse(code: .continueInApp, userActivity: userActivity)) }

    }`

Add a Comment