From 019c2fea6068a03b2715cfe0439e13d3e07e7291 Mon Sep 17 00:00:00 2001 From: Tom Hicks Date: Sun, 27 Jun 2021 13:49:51 -0700 Subject: [PATCH] Fixes opening files from outside the app's sandbox. --- iOS/MonsterCards/Views/ContentView.swift | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/iOS/MonsterCards/Views/ContentView.swift b/iOS/MonsterCards/Views/ContentView.swift index a0e636c..2acb3d5 100644 --- a/iOS/MonsterCards/Views/ContentView.swift +++ b/iOS/MonsterCards/Views/ContentView.swift @@ -53,6 +53,12 @@ struct ContentView: View { let decoder = JSONDecoder() do { + let isAccessing = url.startAccessingSecurityScopedResource() + defer { + if (isAccessing) { + url.stopAccessingSecurityScopedResource() + } + } let data = try Data(contentsOf: url) let monsterDTO = try decoder.decode(MonsterDTO.self, from: data) // TODO: check for some minimal set of properties to ensure this is the expected json schema @@ -60,7 +66,7 @@ struct ContentView: View { // TODO: throw or set an err here and don't set isShowingImportDialog to true if the file didn't match any of our supported monster schemas. self.isShowingImportDialog = true } catch let error as NSError { - // TODO: handle this better + // TODO: show an error message to the user that we were unable to open the file and maybe why. print(error) } }