swift ShareSheet无法打开本地文件

rkue9o1l  于 2023-08-02  发布在  Swift
关注(0)|答案(2)|浏览(628)

我有一个在以前的iOS版本中工作的文件导出器类。现在它抛出一个错误,说文件无法打开。
打开ShareSheet的函数如下所示:

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
    }

字符串
日志上说:

"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


如果我只是尝试String(contentsOf: exportURL),我可以读取文件内容就好了!
我已经想了好几天了,想找你帮忙!
感谢下面的一些评论,我已经用这些要点更新了我的Info.plist,但不幸的是它仍然不起作用:

<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/>

sh7euo9m

sh7euo9m1#

[ShareSheet]无法请求文件的默认共享模式URL:file:/var/mobile/Containers/Data/Application/2E8013C8-DACF-4F53 - 8A73 - 18067C9B1812/Documents/21.07.23_Munich.csv错误:Error Domain = NSOSStatusErrorDomain Code =-10814 "(null)" UserInfo ={_LSLine = 1569,_LSFunction = runEvaluator}[ShareSheet]仅支持CKShare和SWY类型的加载选项。[ShareSheet]获取URL:file:///var/mobile/Containers/Data/Application/2E8013C8-DACF-4F53 - 8A73 - 18067C9B1812/Documents/21.07.23_Munich.csv的项目时出错:错误域= NSCocoaErrorDomain Code = 256 "无法打开文件。"
和/或
[ShareSheet]加载documentURL的元数据时出错:文件:/var/mobile/Containers/Data/Application/2E8013C8-DACF-4F53 - 8A73 - 18067C9B1812/Documents/21.07.23_Munich.csv错误:Error Domain = NSFileProviderInternalErrorDomain Code = 0 "从URL文件中找不到有效的文件提供程序:/var/mobile/Containers/Data/Application/2E8013C8-DACF-4F53 - 8A73 - 18067C9B1812/Documents/21. 07. 23_Munich.csv。"UserInfo ={NSLocalizedDescription =从URL文件中找不到有效的文件提供程序:/var/mobile/Containers/Data/Application/2E8013C8-DACF-4F53 - 8A73 - 18067C9B1812/Documents/21. 07. 23_Munich.csv。{\fnSimHei\bord1\shad1\pos(200,288)}
我相信上面引用的错误与iOS 13的引入有关。对于我自己来说,这些错误并不能阻止ShareSheet显示或保存文件到Files,它只是次优和有点丑陋。完全公开我使用UIKit而不是SwiftUI,我正在导出自定义类型。
Hacking with Swift has a write up on the ShareSheet and SwiftUI, the article seems to target iOS16+)。
也就是说,我认为随着iOS 13的推出,苹果已经收紧了正确使用导出类型的限制。
在你的例子中,你不应该将你的应用程序定义为导出CSV文件,因为你不拥有CSV文件,它们是公共类型,相反,你应该定义你的应用程序可以作为导入/打开CSV文件的目标。From the Apple docs
定义类型时,需要将其定义为导出或导入类型;此声明分别指示您的应用是否是该类型的源,或者它是否支持使用在其他地方定义的类型。如果您的应用使用此框架提供的类型,请不要在应用的bundle中重新声明该类型。
当您的应用是导出类型的规范信息源时,定义该类型。例如,如果您的应用使用自己的专有文档格式,请将其声明为导出类型。
如果您的应用使用另一个应用定义的类型,或者该类型是系统未声明的专有文件格式,请定义导入的类型。从其他应用导入类型时,不要声明自己的标识符;而是使用与原始类型相同的类型标识符。
因此,我会尝试将UIExportedTypeDeclarations替换为UIImportedTypeDeclarations
如果我正确理解文档的话,UITypeIdentifier应该是public.comma-separated-values-text,它的UITypeConformsTo应该是public.delimited-values-text
CSV UTType(注意CSV UTType是在iOS14中引入的)
我还扩展了UIActivityItemSource以 Package URL,然后重载(UIActivityItemSourceactivityViewControllerLinkMetadata并将其传递给UIActivityViewController项目列表。这似乎消除了与文件URL相关的错误。另外,将文件的Data表示而不是URL传递给UIActivityViewController项列表似乎也太麻烦了。(注:LPLinkMetadata在iOS13中引入)

9udxz4iz

9udxz4iz2#

它现在通过使用iOS 16中的新“ShareLink”功能来工作:

ShareLink("Share File", item: fileURL)

字符串

相关问题