I am getting an error of "unable to load saved data" (Xcode 12.3 - Simulator iPhone Xs 14.3) - log shows the following
file:///Users/xxx/Library/Developer/CoreSimulator/Devices/E4CA751B-F8E4-4879-A8A5-247B106C5480/data/Containers/Data/Application/D0EEBC33-1FB9-4A54-BE87-0AE410C44337/Documents/SavedPlaces
Unable to load saved data.
file:///Users/xxx/Library/Developer/CoreSimulator/Devices/E4CA751B-F8E4-4879-A8A5-247B106C5480/data/Containers/Data/Application/D0EEBC33-1FB9-4A54-BE87-0AE410C44337/Documents/SavedPlaces
Unable to load saved data.
Unlocked
2021-01-27 21:13:51.289156+1100 BucketList[1781:64283] Metal API Validation Enabled
ContentView code is.
import LocalAuthentication
import MapKit
import SwiftUI
struct ContentView: View {
@State private var centerCoordinate = CLLocationCoordinate2D()
@State private var locations = [CodableMKPointAnnotation()]
@State private var selectedPlace: MKPointAnnotation?
@State private var showingPlaceDetails = false
@State private var showingEditScreen = false
@State private var isUnlocked = false
var body: some View {
ZStack {
if isUnlocked {
MapView(centerCoordinate: $centerCoordinate, selectedPlace: $selectedPlace, showingPlaceDetails: $showingPlaceDetails, annotations: locations)
.edgesIgnoringSafeArea(.all)
Circle()
.fill(Color.blue)
.opacity(0.3)
.frame(width: 32, height: 32)
VStack {
Spacer()
HStack {
Spacer()
Button(action: {
let newLocation = CodableMKPointAnnotation()
newLocation.coordinate = self.centerCoordinate
newLocation.title = "Example Location"
self.locations.append(newLocation)
self.selectedPlace = newLocation
self.showingEditScreen = true
}) {
Image(systemName: "plus")
}
.padding()
.background(Color.black.opacity(0.75))
.foregroundColor(.white)
.font(.title)
.clipShape(Circle())
.padding(.trailing)
}
}
} else {
Button("Unlock places") {
self.authenticate()
}
.padding()
.background(Color.blue)
.foregroundColor(.white)
.clipShape(Capsule())
}
}
.alert(isPresented: $showingPlaceDetails) {
Alert(title: Text(selectedPlace?.title ?? "Unknown"), message: Text(selectedPlace?.subtitle ?? "Missing place information"), primaryButton: .default(Text("OK")), secondaryButton: .default(Text("Edit")) {
self.showingEditScreen = true
})
}
.sheet(isPresented: $showingEditScreen, onDismiss: saveData) {
if self.selectedPlace != nil {
EditView(placemark: self.selectedPlace!)
}
}
.onAppear(perform: loadData)
}
func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return paths[0]
}
func loadData() {
let filename = getDocumentsDirectory().appendingPathComponent("SavedPlaces")
print(filename)
do {
let data = try Data(contentsOf: filename)
locations = try JSONDecoder().decode([CodableMKPointAnnotation].self, from: data)
} catch {
print("Unable to load saved data.")
}
}
func saveData() {
do {
let filename = getDocumentsDirectory().appendingPathComponent("SavedPlaces")
print(filename)
let data = try JSONEncoder().encode(self.locations)
try data.write(to: filename, options: [.atomicWrite, .completeFileProtection])
print("Data saved")
} catch {
print("Unable to save data.")
}
}
func authenticate() {
let context = LAContext()
var error: NSError?
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
let reason = "Please authenticate yourself to unlock your places."
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { success, autentificationError in
DispatchQueue.main.async {
if success {
self.isUnlocked = true
print("Unlocked")
}
else {
//error
}
}
}
} else {
//no biometrics
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Any clues. Regards