NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

SOLVED: Sharelink problem with CSV file.

Forums > SwiftUI

I'm trying to add the ability to export data from my app in a CSV file via a Sharelink.

The demo code I'm working from, as provided in the "Meet Transferable" WWDC video is as follows:

import CoreTransferable
import UniformTypeIdentifiers

struct ProfilesArchive {
    init(csvData: Data) throws { }
    func convertToCSV() throws -> Data { Data() }

extension ProfilesArchive: Transferable {
    static var transferRepresentation: some TransferRepresentation {
        DataRepresentation(contentType: .commaSeparatedText) { archive in
            try archive.convertToCSV()
        } importing: { data in
            try ProfilesArchive(csvData: data)

This sample code uses the built in ios UTType of .commaSeparatedText.

I have a version of this working, but the file is shared (saved to disk, air dropped, etc) as a .txt file, not a .csv file. Thats not very intuitive for users.

Does anyone have experience with this? Thanks.


Solved this problem by declaring a custom UTType and not using the provided .commaSeparatedText type.

Notes for whoever has this same problem:

  • Create a custom UTType both via an extension to UTType in code and in Info.plist as explained in the WWDC Meet Transferable video.

  • In the Info.plist, the Export Type Identifier has to conform to "". The WWDC video shows "" but that caused an error when sharing the file. The error was "[ShareSheet] Couldn't load file URL for Collaboration Item Provider: ..."

  • If you save the file to a temp directory and transfer it via FileRepresentation, the full filename transfers, whereas using DataRepresentation creates a system generated filename based on your custom UTType. I didn't find a way to control that filename without saving a temp copy first.


Hacking with Swift is sponsored by Judo

SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!

Try now

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

Reply to this topic…

You need to create an account or log in to reply.

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.