iosswiftnsfilemanagermac-catalystuikitformac

Save/create folder that it to be treated as a file with FileManager


I have a iOS/CatalystMacOS-app that can create, save, open custom text-files (with my own file extension). This works fine. However, now I need more than text. I want to save optional files in this file as well. Apparently macOS (and iOS?) can treat folders as files. But I cannot get it to work as wanted. The folder is still treated as a folder, even if it has a file extension.

This is the code I use to create the folder:

func showNewFilePathDialog(from viewController: UIViewController, saveCompleted: URLCallback?) {
    guard !isPresenting else {
        return
    }
    let objectToSave = ...

    // Find an available filename
    var number = 0
    var exportURL: URL!
    var data: Data!
    var fullFileName = ""
    while true {
        let numberText = number == 0 ? "" : number.asString()
        fullFileName = "baseFileName" + "\(numberText).myFileExtension"
        exportURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent(fullFileName)

        let dict = objectToSave.toDict()
        let json = dict.json!
        data = json.data(using: .utf8)!

        if FileManager.default.fileExists(atPath: exportURL.path) {
            number += 1
            continue
        } else {
            break
        }
    }

    do {
        try FileManager.default.createDirectory(atPath: exportURL.path, withIntermediateDirectories: true, attributes: nil)
    } catch {
        NSLog("Couldn't create document directory")
        viewController.presentErrorDialog(from: error)
        return
    }

    // 2. Create containing json file
    do {
        try data.write(to: exportURL.appendingPathComponent("content.json"))
    } catch {
        viewController.presentErrorDialog(from: error)
        return
    }      
    isPresenting = true
    self.onSaveDialogComplete = saveCompleted
    let pickerViewController = UIDocumentPickerViewController(url: exportURL, in: .exportToService)
    pickerViewController.delegate = self
    viewController.present(pickerViewController, animated: true)
}

And then it appears like this in macOS finder:

enter image description here

It will show up similar in iOS, not allowing me to open the folder as a single file either.

Edit: Using UIDocument/public.composite-content/FileWrapper seems to work as well, but the problem still consists: When viewed in macOS finder it is still treated as a folder. Also when trying to open the app from the open-dialog via UIDocumentPickerViewController trying to open the file-bundle only opens the folder and wont let me open it into the app :(

This is my info.list Export Type UTIs: enter image description here

Edit2: Also tried with removing all but com.apple.package but does not work either. Still cannot open my custom type as it behaves like a folder.


Solution

  • Got it working. Seemed as old builds of my app was interfering with the system file types. So I searched for my app name and removed old builds from my computer. Then the system recognized my file suffix and opened it right away!

    But I lost the icon this time, but that's another issue :)