Reputation: 4081
I'm trying to get images that my iOS app writes to its own Documents folder, to appear in the 'Files' app as a folder under 'On my iPhone', and allow them just to be opened as standard images by whatever apps are installed on the phone, like these apps do:
I've set UIFileSharingEnabled
in the Info.plist which means they appear in the 'Music' app on macOS when connecting the phone. However, I haven't been able to get them to appear within the Files app itself.
I see references to UISupportsDocumentBrowser
and LSSupportsOpeningDocumentsInPlace
- but this seems to require building a UI within the app. I've seen other apps do what I want without any custom UI - I just want to expose the files, not do anything 'fancy'.
Any ideas what I'm missing?
Upvotes: 6
Views: 6360
Reputation: 856
As well as UIFileSharingEnabled
add this to your Info.plist
<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>
Here is my test code. Works well for me on macOS 12, using Xcode 13.2, targets: ios-15 and macCatalyst 12. Tested on real ios-15 devices.
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)")
}
}
}
EDIT1: Starting from a "new" project in Xcode.
I created a new ios project from scratch, using Xcode 13.2 on macos 12.2-beta.
Copied and pasted the code in my answer. Then in the Targets->Info
I added the following entries:
Application supports iTunes file sharing
YES
and
Supports opening documents in place
YES
Compiled and ran the project on a iPhone ios-15. In the Files
app,
went to On My iPhone
and the TestApp
folder was there, and inside was the "globe"
image file.
Added the mac catalyst in General -> Deployment Info
,
removed the App sandbox
from the Signing & Capabilities
.
Compiled and ran the project on a iMac macos 12.2, and in the Documents
directory was the globe.png
file.
Upvotes: 13