When I call requestWhenInUseAuthorization() from an Watch App, the Location Services prompt is never displayed to the user: neither in the Watch app, nor in the iPhone app.
- The Support Running Without iOS App Installation is not checked (if I check it, the prompt is correctly displayed, but I don't want my app to work without an iOS companion app).
- I added the
NSLocationWhenInUseUsageDescriptionkey in the info.plist of both the iPhone app and the Watch Extension. - I also tried to add the Location Services in background mode (for both the iPhone and the Watch Extension).
How can we have the prompt displayed on the Watch? Is that even possible? I don't necessarily want to ask my users to open their iPhone app to manage the request location in the app.
import SwiftUI
import CoreLocation
struct ContentView: View {
@StateObject var watchLocationManager = WatchLocManager()
var body: some View {
Button("Request location") { watchLocationManager.requestAuthorization() }
.onAppear {
watchLocationManager.setupLocationManager()
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
class WatchLocManager: NSObject, ObservableObject {
private var locationManager = CLLocationManager()
func setupLocationManager() {
self.locationManager.delegate = self
self.locationManager.activityType = .otherNavigation
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
self.locationManager.distanceFilter = 20.0
}
func requestAuthorization() {
self.locationManager.requestWhenInUseAuthorization()
}
}
extension WatchLocManager: CLLocationManagerDelegate {
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
print("⌚️ locationManagerDidChangeAuthorization: \(manager.authorizationStatus.rawValue)")
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print(locations)
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error)
}
}