'Save to Files' doesn't work with ShareLink with FileRepresentation

I want to use ShareLink+FileRepresentation to save a small text file to my iPhone with the steps below.

  1. Tap [Share...] to display the share sheet. This sheet contains [Save to Files].
  2. Tap [Save to Files].
  3. A black sheet is displayed, but it disappears instantly.

In step 3, I was expecting a browser to be displayed to select the location where the file will be saved. But in fact, a black sheet appears, which quickly disappears.

The implemented code is as follows.

import SwiftUI

@main
struct SLSandboxApp: App {
    var body: some Scene {
        WindowGroup {
            let title = Text("File Output")
            let numListString = "123,456,789"
            let numListFileName = "numlist.csv"
            let tagListFile = TextFile(content: numListString,
                                       filename: numListFileName)
            ShareView(title: title,
                    fileToShare: tagListFile,
                      messageToPreview: numListFileName)
        }
    }
}

struct ShareView: View {
    let title: Text
    let fileToShare: TextFile
    
    let messageToPreview: String
    var body: some View {
        ShareLink(item: self.fileToShare, preview: SharePreview(self.messageToPreview))
    }
}

struct TextFile: Transferable {
    let content: String
    let filename: String
    
    static var transferRepresentation: some TransferRepresentation {
        FileRepresentation(exportedContentType: .data) {
            textFile in
            let data = textFile.content.data(using: .utf8) ?? Data()
            let tempDirectory = FileManager.default.temporaryDirectory
            let fileURL = tempDirectory.appendingPathComponent(textFile.filename)
            try data.write(to: fileURL)
            return SentTransferredFile(fileURL)
        }
    }
}

The development environment is as follows.

  • Xcode 15.4 (Deployment Target = iOS Deployment Target 17.5)
  • macOS 14.6.1

The execution environment is as follows.

  • iPhone SE Gen3 17.7

The following is a console log from the time the application was launched to the time when the share sheet was displayed by tapping [Share...].

Error acquiring assertion: <Error Domain=RBSServiceErrorDomain Code=1 "(originator doesn't have entitlement com.apple.runningboard.primitiveattribute AND originator doesn't have entitlement com.apple.runningboard.assertions.frontboard AND target is not running or doesn't have entitlement com.apple.runningboard.trustedtarget AND Target not hosted by originator)" UserInfo={NSLocalizedFailureReason=(originator doesn't have entitlement com.apple.runningboard.primitiveattribute AND originator doesn't have entitlement com.apple.runningboard.assertions.frontboard AND target is not running or doesn't have entitlement com.apple.runningboard.trustedtarget AND Target not hosted by originator)}>

(501) personaAttributesForPersonaType for type:0 failed with error Error Domain=NSCocoaErrorDomain Code=4099 "The connection to service named com.apple.mobile.usermanagerd.xpc was invalidated: failed at lookup with error 159 - Sandbox restriction." UserInfo={NSDebugDescription=The connection to service named com.apple.mobile.usermanagerd.xpc was invalidated: failed at lookup with error 159 - Sandbox restriction.}

Received port for identifier response: <(null)> with error:Error Domain=RBSServiceErrorDomain Code=1 "Client not entitled" UserInfo={RBSEntitlement=com.apple.runningboard.process-state, NSLocalizedFailureReason=Client not entitled, RBSPermanent=false}
elapsedCPUTimeForFrontBoard couldn't generate a task port
Received port for identifier response: <(null)> with error:Error Domain=RBSServiceErrorDomain Code=1 "Client not entitled" UserInfo={RBSEntitlement=com.apple.runningboard.process-state, NSLocalizedFailureReason=Client not entitled, RBSPermanent=false}
elapsedCPUTimeForFrontBoard couldn't generate a task port
Received port for identifier response: <(null)> with error:Error Domain=RBSServiceErrorDomain Code=1 "Client not entitled" UserInfo={RBSEntitlement=com.apple.runningboard.process-state, NSLocalizedFailureReason=Client not entitled, RBSPermanent=false}

elapsedCPUTimeForFrontBoard couldn't generate a task port

The following is the console log that was written when I tapped [Save to file] on the share sheet.

cancelled request - error: The operation couldn’t be completed. Invalid argument

What modifications should I make to the code to get the expected result?

I encountered this problem as well.

It does not affect iOS 16, but does affect iOS 17 and 18. It's reproducible in the simulator as well as on device.

In my case I was able to use DataRepresentation with a suggestedFileName instead.

In this example, it would look like this:

struct TextFile: Transferable {
    let content: String
    let filename: String

    static var transferRepresentation: some TransferRepresentation {
        DataRepresentation(exportedContentType: .data) {
            textFile in
            return textFile.content.data(using: .utf8) ?? Data()
        }
        .suggestedFileName {
            textFile in
            return textFile.filename
        }
    }
}

Caveats:

  • Requires iOS 17+ if you want to use this suggestedFileName method.
  • Isn't feasible for large files that won't fit in memory.
'Save to Files' doesn't work with ShareLink with FileRepresentation
 
 
Q