簡體   English   中英

在 Files 應用程序中顯示 iOS 應用程序文件

[英]Showing iOS app files within in the Files app

我正在嘗試獲取我的 iOS 應用程序寫入其自己的 Documents 文件夾的圖像,以作為“在我的 iPhone”下的文件夾出現在“文件”應用程序中,並允許它們作為標准圖像被任何應用程序打開安裝在手機上,就像這些應用程序一樣:

在此處輸入圖像描述

我在 Info.plist 中設置了UIFileSharingEnabled ,這意味着它們在連接手機時出現在 macOS 的“音樂”應用程序中。 但是,我無法讓它們出現在“文件”應用程序本身中。

我看到了對UISupportsDocumentBrowserLSSupportsOpeningDocumentsInPlace的引用——但這似乎需要在應用程序中構建一個 UI。 我已經看到其他應用程序在沒有任何自定義 UI 的情況下做我想做的事——我只想公開文件,而不是做任何“花哨”的事情。

有什么我想念的想法嗎?

以及UIFileSharingEnabled添加到您的Info.plist

<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>

這是我的測試代碼。 在 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)")
        }
    }
    
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM