TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

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.

2      

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 "public.data". The WWDC video shows "com.public.data" 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.

3      

Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

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.