I'm a new developer and I'm trying to learn how to develop a very basic Apple Watch app (without an additional iOS app) that displays the user's current gps location, as part of a more complicated project. When it comes time to run the app, I get the following error:
"This app has attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an “NSLocationWhenInUseUsageDescription” key with a string value explaining to the user how the app uses this data"
This was interesting for me because there is no info.plist file in my project, and it seems like the newer versions of Xcode don't automatically generate this file for you. So, I tried to create my own property list file, name it info.plist, and include the “NSLocationWhenInUseUsageDescription” key/value in it. Unfortunately, this did not do the trick. I'm assuming, because I'm a beginner, that this is a relatively easy fix.
I will provide the my code for context below, any advice would be greatly appreciated, I'm a bit overwhelmed by all the information out there, and a lot of it seems outdated and not helpful to me.
ContentView.swift
import SwiftUI
import CoreLocation
struct ContentView: View {
@StateObject private var locationManager = LocationManager()
var body: some View {
VStack {
Text("Your Location")
.font(.largeTitle)
if let location = locationManager.currentLocation {
Text("Latitude: \(location.coordinate.latitude)")
Text("Longitude: \(location.coordinate.longitude)")
} else {
Text("Location not available")
}
}
.padding()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
LocationManager.swift
import Foundation
import CoreLocation
class LocationManager: NSObject, ObservableObject {
@Published var currentLocation: CLLocation?
private let locationManager = CLLocationManager()
override init() {
super.init()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
}
extension LocationManager: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return }
currentLocation = location
}
}
Screenshot of Info.plist
(xcode changes it after I enter 'NSLocationWhenInUseUsageDescription')