UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How do I create my own iOS custom file format in Swift?

Forums > Swift

I want to create my own file format for an app I am developing for iOS.

My requisites are:

  • files have the extension .greatApp
  • .greatApp files are in fact what Apple calls a package, like macOS' .app files are on macOS, that are disguised folders/zip.
  • inside that package I will have a thumbnail, a JSON text file and an image.

I have configured Target/Info like this on Xcode.

I have run the app on the device and now, at least, iOS Files.app shows the icon I have defined for the package when showing files with extension .greatApp. But long pressing the file does not show open on Great App.

This is the first problem.

Another problem is how, inside the app, I create that container and start adding stuff to it.

I could simply create a folder with the desired extension and add stuff to it, but I read months ago, when I read an article about it, that there is another way, but I cannot find anything about it.

I don't know where to start.

Thanks

3      

On your first issue, you need to add the relevant details to CFBundleURLSchemes in your info.plist, you can find an article on that here: http://swiftdeveloperblog.com/deep-linking-using-custom-url-scheme/

When it comes to creating a container, you need a struct for that container with export and import methods. The code below is from one of my apps, where I have a template file with the extension .mdft that can be shared outside of the app. I'm using a pod called Disk here to save the template in this case, which I wouldn't do if I was writing this today.


import Foundation
import Disk

struct Template: Codable {
    var height: Int
    var blocks: [Block]
    var filename: String
    var font: String
    var fontSize: Int
    var previewImage: SavableImage?

    func exportToURL() -> URL? {
        // 1
        guard let encoded = try? JSONEncoder().encode(self) else { return nil }

        // 2
        let documents = FileManager.default.urls(
            for: .documentDirectory,
            in: .userDomainMask
            ).first

        guard let path = documents?.appendingPathComponent("/\(filename).mdft") else {
            return nil
        }

        // 3
        do {
            try encoded.write(to: path, options: .atomicWrite)
            return path
        } catch {
            print(error.localizedDescription)
            return nil
        }
    }

    static func importData(from url: URL) {
        // 1
        guard
            let data = try? Data(contentsOf: url),
            let template = try? JSONDecoder().decode(Template.self, from: data)
            else { return }

            do {
                var templateFilenames = try Disk.retrieve("TemplateFilenames", from: .documents, as: [String].self)

                var templateToSave = template

                if templateFilenames.contains(templateToSave.filename) {
                        templateToSave.filename = templateToSave.filename + "1"
                    }

                try Disk.save(templateToSave, to: .documents, as: templateToSave.filename)

                templateFilenames.append(templateToSave.filename)
                    try Disk.save(templateFilenames, to: .documents, as: "TemplateFilenames")

            } catch {
                print("could not load template from share")
            }

        // 3
        try? FileManager.default.removeItem(at: url)
    }

}

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.