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

New ShareLink with custom type is not working

Forums > SwiftUI

Hi everyone!

I want to share audio files with the new ShareLink in SwiftUI. I've a Recording entity from Core Data, witch store the URL from the audio file and the file itself is store in the FileManger. I already make Recording to conform Transferable protocol.

But in the line of the Sharelink appears an error compiler: "No exact matches in call to initializer".

Here is the code:

Recording entity:

extension Recording {

    @nonobjc public class func fetchRequest() -> NSFetchRequest<Recording> {
        return NSFetchRequest<Recording>(entityName: "Recording")
    }

    @NSManaged public var date: Date
    @NSManaged public var id: UUID
    @NSManaged public var url: String
    @NSManaged public var title: String

}

extension Recording : Identifiable, Transferable {
    // Transferable protocol
    static var containerUrl = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)

    public static var transferRepresentation: some TransferRepresentation {
        FileRepresentation(exportedContentType: .audio) { audio in
            SentTransferredFile(URL(string: audio.url)!)
        }
    }
}

View:

struct AudioPlayerView: View {

@ObservedObject var recording: Recording

  var body: some View {
        NavigationStack {
            VStack(spacing: 20){

                VStack {
                    Text(recording.title)
                        .font(.title)
                        .bold()
                    Text("\(recording.date, format: .dateTime)")
                        .foregroundColor(.secondary)
                }
             }
        }
     .toolbar {
           ToolbarItem(placement: .navigationBarLeading) {
               ShareLink(item: recording) { // This line gives the error: No exact matches in call to initializer 
                   Image(systemName: "square.and.arrow.up")
               }
           }
   }

Any idea? I have tried to simplify the code so let me know if I have forgotten something. Thanks!

2      

I realize this is a very late response, but I ran into this yesterday..

Providing a preview in the ShareLink call cleared the error. It must be required for a custom item.

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.