Request location only on Apple Watch, no code on paired phone

I looked everywhere, including an example Apple application . But I can’t find anywhere when I can request a location without having to run the code on the phone. The code below, when used in the "interface controller", returns a "not defined" state. I have verified that the privacy key values ​​are set in my info.plist.

import Foundation import CoreLocation class LocationManager: NSObject, CLLocationManagerDelegate { /// Location manager to request authorization and location updates. let manager = CLLocationManager() /// Flag indicating whether the manager is requesting the user location. var isRequestingLocation = false var workoutLocation: CLLocation? func requestLocation() { guard !isRequestingLocation else { manager.stopUpdatingLocation() isRequestingLocation = false return } let authorizationStatus = CLLocationManager.authorizationStatus() switch authorizationStatus { case .notDetermined: isRequestingLocation = true manager.requestWhenInUseAuthorization() case .authorizedWhenInUse: isRequestingLocation = true manager.requestLocation() case .denied: print("Location Authorization Denied") default: print("Location AUthorization Status Unknown") } } func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { guard !locations.isEmpty else { return } DispatchQueue.main.async { let lastLocationCoordinate = locations.last!.coordinate print("Lat = \(lastLocationCoordinate.latitude)") print("Long = \(lastLocationCoordinate.longitude)") self.isRequestingLocation = false } } } 

Main application info.plist

 <key>NSLocationAlwaysUsageDescription</key> <string>We will read your location while performing a workout to create a workout route</string> <key>NSLocationUsageDescription</key> <string>We will read your location while performing a workout to create a workout route</string> <key>NSLocationWhenInUseUsageDescription</key> <string>We will read your location while performing a workout to create a workout route</string> 

See extension info.plist

 <key>NSLocationAlwaysUsageDescription</key> <string>We will read your location while performing a workout to create a workout route</string> <key>NSLocationUsageDescription</key> <string>We will read your location while performing a workout to create a workout route</string> <key>NSLocationWhenInUseUsageDescription</key> <string>We will read your location while performing a workout to create a workout route</string> 
+9
source share
2 answers

A user can only provide access to his location on his iPhone. This cannot be done on the Apple Watch. If the iPhone to which the Watch is connected is unlocked, a request will be displayed on the phone asking for authorization to use the location; you do not need to run any code for this on iOS.

From watchOS Application Programming Guide: Using iOS Technologies

Keep in mind that permission for some technologies must be taken by iPhone users. The user must grant permission to use a specific technology system, such as Core Location. Using one of these technologies in your extension, WatchKit launches a corresponding invitation to iPhone users. Apple Watch also displays a prompt, prompting the user to view the permission request on the iPhone. For information on technologies that require user permission, see "User Privacy Support" in the iOS Application Programming Guide.

+11
source

I made it work (using Xcode 10.2.1, iOS 12.4, WatchOS 5.3), but only after adding the necessary keys ( NSLocationAlwaysAndWhenInUseUsageDescription and NSLocationWhenInUseUsageDescription ) in Info.plist for the iPhone application. Initially, I defined them only in the extension of the Watch application, and in this case, the request for authorization of the location did not display anything on the phone. After I added them to the iPhone Info.plist, I got the expected login dialog for the iPhone when I requested authorization from the Watch application, without adding any additional code to the iPhone application.

Perhaps it was a mistake in previous versions, or maybe it was using outdated versions of key names ?

0
source

Source: https://habr.com/ru/post/1273728/


All Articles