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

Adding icon and file description on share sheet ActivityViewController

Forums > SwiftUI

I'm exporting CSV sheets from my CoreData entities, creating comma separated Strings, and appending them to exporting functions like this:

func dataExport(data: Student) -> [Any] {

    let exportFilePath = NSTemporaryDirectory() + "student tutorships.csv"
    let exportFileURL = URL(fileURLWithPath: exportFilePath)
    FileManager.default.createFile(atPath: exportFilePath, contents: Data(), attributes: nil)
    let fileHandle: FileHandle?

    do {
        fileHandle = try FileHandle(forWritingTo: exportFileURL)
    } catch let error as NSError {
        print("Error writing exporting file: \(error)")
        fileHandle = nil

    if let fileHandle = fileHandle {

        let headers = "Date, Attendees, Topics, Conclussions\r\n"
        let headersData = .utf8, allowLossyConversion: false)!

        //csvStudentTutorship is the creating-CSV function.
        guard let csvData = data.csvStudentTutorships().data(using: .utf8, allowLossyConversion: false) else { return [] }
        let document = NSURL(fileURLWithPath: exportFilePath)
        let fileToShare = [document]
        return fileToShare
    return []

This way, I get and array of type NSURL as output to be shared:

let fileToShare: [NSURL]

For sharing sheet to be presented in SwiftUI, I use an ActivityViewController with UIViewControllerRepresentable protocol:

import UIKit
import SwiftUI

struct ActivityViewController: UIViewControllerRepresentable {

    var activityItems: [Any]
    var applicationActivities: [UIActivity]? = nil

    func makeUIViewController(context: UIViewControllerRepresentableContext<ActivityViewController>) -> UIActivityViewController {

        let controller = UIActivityViewController(activityItems: activityItems, applicationActivities: applicationActivities)
        return controller

    func updateUIViewController(_ uiViewController: UIActivityViewController, context: Context) {

And I call an instance from my view with

.sheet(isPresented: $showingSharing) {
            ActivityViewController(activityItems: studentTutorshipsExport(data: student), applicationActivities: nil)

All that is working correctly (many console messages, but working).

Now, I'm trying to get my app logo and the description of the sharing file with activityViewControllerLinkMetadata as described here: and here

But I can't make it work. If I pass the icon image from my origin view as "activityItems" parameter , only image appears, but I loose sharing document itself. I'm not sure if this is correct in SwiftUI. Any further ideas?


Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

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.