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

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 = headers.data(using: .utf8, allowLossyConversion: false)!
        fileHandle.write(headersData)

        fileHandle.seekToEndOfFile()
        //csvStudentTutorship is the creating-CSV function.
        guard let csvData = data.csvStudentTutorships().data(using: .utf8, allowLossyConversion: false) else { return [] }
        fileHandle.write(csvData)
        fileHandle.closeFile()
        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: https://stackoverflow.com/questions/63886792/how-to-attach-an-image-icon-to-the-uiactivityviewcontroller and here https://developer.apple.com/documentation/uikit/uiactivityitemsource/3144571-activityviewcontrollerlinkmetada?language=swift.

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?

1      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out 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.