0

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.

Blazej SLEBODA
  • 8,936
  • 7
  • 53
  • 93
LucasC
  • 43
  • 5

1 Answers1

0

There are many ShareLink initializers, but not all permutations are implemented.

Your initializer code is similar to some listed under "Sharing an item", "Sharing an item with a label", and "Sharing an item with a preview", but it doesn't exactly match them.

You need to exactly match what the documentation is asking for to make it compile.

One option is to add a preview: derived from your object's data.

ShareLink(item: recording, preview: SharePreview(Text(recording.title))) { 
    Image(systemName: "square.and.arrow.up")
}

Otherwise, if there is a Label (and no SharePreview), the type of item: must be String or URL.

pioneer78
  • 683
  • 5
  • 11
  • I have tried with the `preview` option, it throws me an error `NSExtension] Extension request contains input items but the extension point does not specify a set of allowed payload classes. The extension point's NSExtensionContext subclass must implement `+_allowedItemPayloadClasses`. This must return the set of allowed NSExtensionItem payload classes. In future, this request will fail with an error.`and the ShareSheet never shows up. If i try without preview and I pass an URL to `item`, then the share sheet shows up but only share a plain text with the URL of the audio inside, no audio file. – LucasC Sep 29 '22 at 16:33
  • I think that error you mention is shown even when the ShareLink is working correctly. I still see it in the console. – pioneer78 Oct 01 '22 at 08:06
  • Hmm... weird, so I don't know why the share sheet is not being displayed. And with the other alternative, the share sheet shows up but with a plain text file with the URL write inside. Any idea? – LucasC Oct 01 '22 at 12:10
  • Try rebuilding from a new CoreData iOS App project template? I tried to recreate the problem and wasn't able to (the share sheet always showed up). There's hope! – pioneer78 Oct 02 '22 at 06:31