0

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.

4

1 回答 1

1

得到它的工作。似乎我的应用程序的旧版本干扰了系统文件类型。所以我搜索了我的应用名称并从我的计算机中删除了旧版本。然后系统识别出我的文件后缀并立即打开!

但是这次我丢失了图标,但这是另一个问题:)

于 2019-12-01T20:40:49.357 回答