简体   繁体   中英

Swift - Import and open custom file extensions

I have to open a custom file within the app I am building but I am not sure how to do this. The file imported just contains folders within it which contains a plist

for example fileName.customExtension/name uuid/name.plist

if I open the custom file copy and paste the other folder "name uuid" into a regular folder which then I import in the app it works fine.

for example fileName/name uuid/name.plist

So how could i import the folder with the custom extension directly in the app?

I wrote the code below if the custom file is dropped in to the app to test at the moment but it doesn't work as I am guessing I am not converting the extension into a regular folder but instead just removing it.

func loadImportedFiles(){

    var fileName: String = ""
    let documentsDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
    do {
        let fileUrls = try FileManager.default.contentsOfDirectory(at: documentsDirectoryURL, includingPropertiesForKeys: nil)
        for plist in fileUrls {
            let path: String = plist.path
            let file = URL(fileURLWithPath: path).lastPathComponent

            if file.contains(".CustomExtension"){
                fileName = URL(fileURLWithPath: path).deletingPathExtension().lastPathComponent
            }
        }
    } catch {
        print("error path")
    }

    var objCBool: ObjCBool = true
    let mainPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0];
    let folderPath = mainPath + "/Folder/\(fileName)"

    let isExist = FileManager.default.fileExists(atPath: folderPath, isDirectory: &objCBool)
    if !isExist {
        do {
            try FileManager.default.createDirectory(atPath: folderPath, withIntermediateDirectories: true, attributes: nil)
        } catch {
            print(error)
        }
    }


    let originalDataFilePath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent("\(fileName).CustomExtension")
    let newDestinationPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent("Folder/\(fileName)")

    do { try FileManager.default.moveItem(at: originalDataFilePath!, to: newDestinationPath!)
        print("imported")
    } catch {
        print(error)
    }


}

If someone could guide me in the right direction that would be great thanks!

The solution is to use Custom File Types in the Info Plist. You will need to add "Exported Type UTIs" and "Document types" to the Info Plist. Then your custom files will be recognised.

Below is the tutorial I used to implement it.

https://chariotsolutions.com/blog/post/importing-data-via-custom-file-types-in/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM