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

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 RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.