在“文件”应用程序中显示 iOS 应用程序文件

Jam*_*ley 6 ios swift

我正在尝试获取我的 iOS 应用程序写入其自己的文档文件夹的图像,以作为“在我的 iPhone 上”下的文件夹出现在“文件”应用程序中,并允许它们作为标准图像由任何应用程序打开安装在手机上,就像这些应用程序一样:

在此输入图像描述

我已经UIFileSharingEnabled在 Info.plist 中进行了设置,这意味着连接手机时它们会出现在 macOS 上的“音乐”应用程序中。但是,我无法让它们出现在“文件”应用程序本身中。

UISupportsDocumentBrowser我看到对和 的引用LSSupportsOpeningDocumentsInPlace- 但这似乎需要在应用程序内构建一个用户界面。我见过其他应用程序在没有任何自定义 UI 的情况下完成我想要的操作 - 我只想公开文件,而不是做任何“花哨”的事情。

我有什么想法吗?

dud*_*tte 13

以及UIFileSharingEnabled将其添加到您的Info.plist

<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>
Run Code Online (Sandbox Code Playgroud)

这是我的测试代码。在 macOS 12 上运行良好,使用 Xcode 13.2,目标:ios-15 和 macCatalyst 12。在真实的 ios-15 设备上进行了测试。

import SwiftUI

@main
struct TestApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

    struct ContentView: View {
    let image = UIImage(systemName: "globe")!
    var body: some View {
        Image(uiImage: image).resizable().frame(width: 111, height: 111)
        Button(action: {
            saveImage(file: "globe")
        }) {
            Text("save test file")
        }
    }
    
    func saveImage(file: String) {
        do {
            let fileURL = try FileManager.default
                .url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
                .appendingPathComponent(file)
                .appendingPathExtension("png")
            try image.pngData()?.write(to: fileURL)
        } catch {
            print("could not create file: \(file)")
        }
    }
    
}
Run Code Online (Sandbox Code Playgroud)

EDIT1:从 Xcode 中的“新”项目开始。

我在 macos 12.2-beta 上使用 Xcode 13.2 从头开始​​创建了一个新的 ios 项目。将代码复制并粘贴到我的答案中。然后在Targets->Info

我添加了以下条目:

Application supports iTunes file sharing是的

Supports opening documents in place是的

在 iPhone ios-15 上编译并运行该项目。在Files应用程序中,转到On My iPhone文件TestApp夹,里面是“地球”图像文件。

在 中添加了 mac 催化剂,从 中General -> Deployment Info删除了。App sandboxSigning & Capabilities

在iMac macos 12.2上编译并运行该项目,目录中Documents 有该globe.png文件。