GarySabo GarySabo - 1 year ago 108
Swift Question

Request location on the Apple Watch only, without code on the paired phone

I've looked everywhere including Apple's sample app.
But I can't find anywhere when I can request location without needing any code running on the phone, the below code when used in an 'interface controller' returns a "not determined" status. I did check that my info.plist has the privacy key values set.

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's 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 App 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>


Watch 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>

Answer Source

The user can only grant access to their location on their iPhone. It cannot be done on the Apple Watch. If the iPhone to which the Watch is connected is unlocked, then the prompt asking for location usage authorization will be displayed on the phone; you don't need to run any code for this on iOS.

From the App Programming Guide for watchOS: Leveraging iOS Technologies

Be aware that permission for some technologies must be accepted on the user’s iPhone. The user must grant permission to use specific system technologies, such as Core Location. Using one of these technologies in your WatchKit extension triggers the appropriate prompt on the user’s iPhone. Apple Watch also displays a prompt of its own, asking the user to view the permission request on the iPhone. For information about the technologies that require user permission, see “Supporting User Privacy” in App Programming Guide for iOS.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download