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>
Note: If your friend is wearing an Apple Watch with cellular and is sharing their location, but they don't have their iPhone with them, their location will be tracked using their Apple Watch.
Location-Based Alerts: Your iPhone and Apple Watch will use your location in order to provide you with geographically relevant alerts, such as a reminder to call someone when you get to a specific place, when to leave for your next appointment, or an app or shortcut recommendation based on where you currently are.
Question: Q: Tracking runs on apple watch without cellularYes, it is able to do that. Apple Watch Series 3 (GPS) has built-in GPS, which does not require the paired iPhone to be taken along when running.
As of watchOS 6.0/ Xcode 11, this is now possible. To enable it, the watch extension target must have the box checked for Supports running without iOS App installation
After checking this setting and rebuilding, my requestWhenInUseAuthorization
call showed the prompt on the watch face as expected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With