Skip to content

Commit 919bc9c

Browse files
committed
With iOS 17 the url must point to a temporary file. Previously the url referred directly to a file within the application bundle.
1 parent 16c922f commit 919bc9c

2 files changed

Lines changed: 30 additions & 7 deletions

File tree

Source/TriangleDrawLibrary/Engine/Document.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ public class Document: UIDocument {
3131

3232
override public func contents(forType typeName: String) throws -> Any {
3333
guard let canvas: E2Canvas = self.canvas else {
34+
log.error("Document.contents(forType:) canvas is nil")
3435
throw DocumentError.archivingFailure
3536
}
3637
let data: Data = TDCanvasWriter.pbmRepresentation(from: canvas)
3738
guard !data.isEmpty else {
39+
log.error("Document.contents(forType:) data is nil")
3840
throw DocumentError.archivingFailure
3941
}
4042
return data

Source/TriangleDrawMain/Browser/BrowserViewController.swift

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,28 @@ extension BrowserViewController: UIDocumentBrowserViewControllerDelegate {
215215
_ controller: UIDocumentBrowserViewController,
216216
didRequestDocumentCreationWithHandler importHandler: @escaping (URL?, UIDocumentBrowserViewController.ImportMode) -> Void) {
217217

218-
// When the user wants to create a new document, a blank version of a new Partiles file needs to be provided to the
219-
// `UIDocumentBrowserViewController`. In this case, obtain the URL of the "Drawing.triangleDraw", which is part of the application bundle, and
220-
// afterwards, perform the importHandler on the URL with a Copy operation.
221-
222-
let url: URL = DocumentExample.blankFile.url
223-
log.debug("importHandler mode_copy with url: \(url)")
224-
importHandler(url, .copy)
218+
// Get url to "Drawing.triangleDraw", which is part of the application bundle.
219+
let originalURL: URL = DocumentExample.blankFile.url
220+
221+
// Create a temporary file url
222+
guard let temporaryFileURL: URL = createTemporaryFile() else {
223+
log.error("Failed to create temporary file")
224+
importHandler(nil, .none)
225+
return
226+
}
227+
228+
// Save a copy at the temporary location
229+
do {
230+
try FileManager.default.copyItem(at: originalURL, to: temporaryFileURL)
231+
} catch {
232+
log.error("Failed to copy file: \(error)")
233+
importHandler(nil, .none)
234+
return
235+
}
236+
237+
// Import the file
238+
log.debug("File copied from \(originalURL) to \(temporaryFileURL)")
239+
importHandler(temporaryFileURL, .move)
225240
}
226241

227242
/// Called when you select an existing document in the browser.
@@ -262,5 +277,11 @@ extension BrowserViewController: UIDocumentBrowserViewControllerDelegate {
262277
// with custom ones. In this case, no additional activities are added.
263278
return []
264279
}
280+
}
265281

282+
func createTemporaryFile() -> URL? {
283+
let temporaryDirectory = FileManager.default.temporaryDirectory
284+
let uniqueFilename = UUID().uuidString
285+
let temporaryFileURL = temporaryDirectory.appendingPathComponent(uniqueFilename).appendingPathExtension("triangleDraw")
286+
return temporaryFileURL
266287
}

0 commit comments

Comments
 (0)