Posts

Post not yet marked as solved
0 Replies
796 Views
I'm working on an App Shortcut using the new AppIntents framework in iOS 16 and I'm trying to get the user's current location, everything is enabled and set-up correctly with the permissions func perform() async throws -> some IntentResult { //Request User Location IntentHelper.sharedInstance.getUserLocation() guard let userCoords = IntentHelper.sharedInstance.currentUserCoords else { throw IntentErrors.locationProblem } //How to wait for location?? return .result(dialog: "Worked! Current coords are \(userCoords)") { IntentSuccesView() } } And here is the IntentHelper class class IntentHelper: NSObject { static let sharedInstance = IntentHelper() var currentUserCoords: CLLocationCoordinate2D? private override init() {} func getUserLocation() { DispatchQueue.main.async { let locationManager = CLLocationManager() locationManager.delegate = self print("FINALLY THIS IS IT") self.currentUserCoords = locationManager.location?.coordinate print(self.currentUserCoords) } } } extension IntentHelper: CLLocationManagerDelegate { func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { print(error) manager.stopUpdatingLocation() } } Problem is, this sometimes, very rarely works, most of the times it prints nil, so how would you go about waiting for the location?
Posted
by David861.
Last updated
.