@@ -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