swiftios16ios-sharesheet

ShareSheet fails to open a local file


I have a file-exporter class that was working in previous iOS versions. Now it is throwing an error saying the file can't be opened.

The function that opens the ShareSheet looks like this:

func share(log: Log, fileType: String, excludedActivityTypes: [UIActivity.ActivityType]? = nil
    ) {
        guard let source = (UIApplication.shared.connectedScenes.first as? UIWindowScene)?.windows.first?.rootViewController else {
            return
        }
        
        DispatchQueue.main.async {
            self.isSharing = true
        }
    
        let filename = dateFormatterShort.string(from: log.date) + "_\(log.takeOff)"
        let exportURL = save(filename, fileContents: fileContents, fileExt: fileExt)

        DispatchQueue.main.async {
            let vc = UIActivityViewController(
                activityItems: [exportURL],
                applicationActivities: nil
            )
            vc.excludedActivityTypes = excludedActivityTypes
            vc.popoverPresentationController?.sourceView = source.view
            source.present(vc, animated: true)
            self.isSharing = false
        }
        return
    }

The log says:

"Writing CSV file with 11 entries."
"Saved file to: file:///var/mobile/Containers/Data/Application/2E8013C8-DACF-4F53-8A73-18067C9B1812/Documents/21.07.23_Munich.csv"
[ShareSheet] Failed to request default share mode for fileURL:file:///var/mobile/Containers/Data/Application/2E8013C8-DACF-4F53-8A73-18067C9B1812/Documents/21.07.23_Munich.csv error:Error Domain=NSOSStatusErrorDomain Code=-10814 "(null)" UserInfo={_LSLine=1569, _LSFunction=runEvaluator}
[ShareSheet] Only support loading options for CKShare and SWY types.
[ShareSheet] error fetching item for URL:file:///var/mobile/Containers/Data/Application/2E8013C8-DACF-4F53-8A73-18067C9B1812/Documents/21.07.23_Munich.csv : Error Domain=NSCocoaErrorDomain Code=256 "The file couldn’t be opened."
[default] LaunchServices: store (null) or url (null) was nil: Error Domain=NSOSStatusErrorDomain Code=-54 "process may not map database" UserInfo={NSDebugDescription=process may not map database, _LSLine=66, _LSFunction=_LSServer_GetServerStoreForConnectionWithCompletionHandler}
[default] Attempt to map database failed: permission was denied. This attempt will not be retried.
[db] Failed to initialize client context with error Error Domain=NSOSStatusErrorDomain Code=-54 "process may not map database" UserInfo={NSDebugDescription=process may not map database, _LSLine=66, _LSFunction=_LSServer_GetServerStoreForConnectionWithCompletionHandler}
[ShareSheet] error loading metadata for documentURL:file:///var/mobile/Containers/Data/Application/2E8013C8-DACF-4F53-8A73-18067C9B1812/Documents/21.07.23_Munich.csv error:Error Domain=NSFileProviderInternalErrorDomain Code=0 "No valid file provider found from URL file:///var/mobile/Containers/Data/Application/2E8013C8-DACF-4F53-8A73-18067C9B1812/Documents/21.07.23_Munich.csv." UserInfo={NSLocalizedDescription=No valid file provider found from URL file:///var/mobile/Containers/Data/Application/2E8013C8-DACF-4F53-8A73-18067C9B1812/Documents/21.07.23_Munich.csv.}
[Presentation] Attempt to present <UIActivityViewController: 0x10500ea00> on <_TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier__: 0x105019e00> (from <_TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier__: 0x105019e00>) which is already presenting <_TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView_: 0x103810000>.
[default] LaunchServices: store (null) or url (null) was nil: Error Domain=NSOSStatusErrorDomain Code=-54 "process may not map database" UserInfo={NSDebugDescription=process may not map database, _LSLine=66, _LSFunction=_LSServer_GetServerStoreForConnectionWithCompletionHandler}
[default] Attempt to map database failed: permission was denied. This attempt will not be retried.
[db] Failed to initialize client context with error Error Domain=NSOSStatusErrorDomain Code=-54 "process may not map database" UserInfo={NSDebugDescription=process may not map database, _LSLine=66, _LSFunction=_LSServer_GetServerStoreForConnectionWithCompletionHandler}
[ShareSheet] connection invalidated

If I simply try String(contentsOf: exportURL) I can read the file content just fine!

I have tried to find a solution for days now and would like to ask you for help!

Thanks to some comments below, I have updated my Info.plist with these points, but it still doesn't work unfortunately:

    <key>CFBundleDocumentTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeName</key>
            <string>CSV File</string>
            <key>LSItemContentTypes</key>
            <array>
                <string>com.lcairsports.csv</string>
            </array>
        </dict>
    </array>
    <key>UTExportedTypeDeclarations</key>
    <array>
        <dict>
            <key>UTTypeConformsTo</key>
            <array>
                <string>public.comma-separated-values-text</string>
            </array>
            <key>UTTypeIdentifier</key>
            <string>com.lcairsports.csv</string>
            <key>UTTypeDescription</key>
            <string>CSV File</string>
            <key>UTTypeTagSpecification</key>
            <dict>
                <key>public.filename-extension</key>
                <string>csv</string>
            </dict>
        </dict>
    </array>
    <key>UIFileSharingEnabled</key>
    <true/>

Solution

  • It works now by using the new "ShareLink" functionality presented in iOS 16:

    ShareLink("Share File", item: fileURL)