Thanks for being a part of WWDC25!

How did we do? We’d love to know your thoughts on this year’s conference. Take the survey here

Failed to create folder using Swift in VisionOS

When creating a folder in the code, it prompts that the file creation is successful, but when the folder does not exist in the "Download Container" file, do you have any permissions when creating the folder in VisionOS?

static func getFileManager() -> URL {
        let documentsDirectory = FileManager.default.urls(
            for: .documentDirectory,
            in: .userDomainMask
        ).first!
        return documentsDirectory.appendingPathComponent("SGKJ_LIBRARY")
    }

    static func createFileLibrary() {
        let folderUrl = getFileManager()
        let fileManager = FileManager.default
        do {
            try fileManager.createDirectory(
                at: folderUrl,
                withIntermediateDirectories: true,
                attributes: nil
            )
            print("Folder created successfully: \(folderUrl.path)")
        } catch {
            print("Failed to create folder: \(error.localizedDescription)")
        }
    }
Answered by DTS Engineer in 836838022

I’d like to clarify the problem you’re reporting here. My understanding is that you’re seeing this:

  1. You put that code into an app and run it.

  2. It’s able to successfully create the directory, meaning you see the Folder created successfully output.

  3. You then stop the app.

  4. In Xcode, you go to the Devices and Simulators window.

  5. Select the Devices tab.

  6. Select your device on the left.

  7. Select your app in the Installed Apps list.

  8. And from the action () menu, choose Download Container.

  9. In the Finder, you select the resulting .xcappdata.

  10. And control-click it and choose Show Package Containers.

  11. You explore the container’s content but fail to find the SGKJ_LIBRARY directory.

Is that right?

If so, I think that’s expected. Well, more like working as designed. The Download Container facility doesn’t include empty folders.

If you add this to your code:

let file = folderUrl.appending(component: "test.txt")
try "\(Date())".write(to: file, atomically: true, encoding: .utf8)

the test.txt is present in the downloaded container, bringing with it the parent SGKJ_LIBRARY directory.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

I’d like to clarify the problem you’re reporting here. My understanding is that you’re seeing this:

  1. You put that code into an app and run it.

  2. It’s able to successfully create the directory, meaning you see the Folder created successfully output.

  3. You then stop the app.

  4. In Xcode, you go to the Devices and Simulators window.

  5. Select the Devices tab.

  6. Select your device on the left.

  7. Select your app in the Installed Apps list.

  8. And from the action () menu, choose Download Container.

  9. In the Finder, you select the resulting .xcappdata.

  10. And control-click it and choose Show Package Containers.

  11. You explore the container’s content but fail to find the SGKJ_LIBRARY directory.

Is that right?

If so, I think that’s expected. Well, more like working as designed. The Download Container facility doesn’t include empty folders.

If you add this to your code:

let file = folderUrl.appending(component: "test.txt")
try "\(Date())".write(to: file, atomically: true, encoding: .utf8)

the test.txt is present in the downloaded container, bringing with it the parent SGKJ_LIBRARY directory.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Failed to create folder using Swift in VisionOS
 
 
Q