How can I display my App documents in the Files app for iPhone
If you would like to expose your App document files inside Apple's Files App you need to include the "Supports Document Browser"
key in your info plist file and set it to YES:
Showing iOS app files within in the Files app
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.
App's files are Visible in 'Files' app on simulator but not on device
Restarting the iPhone fixed it!
I can only guess as to why. Perhaps iOS had cached the capabilities of an earlier version of the app which did have 'Files' access and perhaps uninstalling and re-installing the app may have also resolved it (certainly just re-installing the app by running from XCode did NOT resolve it.
But simply restarting the iPhone, then opening the 'Files' app showed the app's folder and all the files therein.
Open iOS 11 Files app and display my app's Documents folder
you can open the Files app with the scheme shareddocuments
var url = new NSUrl("shareddocuments://" + Uri.EscapeDataString(folderPath));
UIApplication.SharedApplication.OpenUrl(url);
Share Specific directory in app's Documents with Files app
Before using file system of iOS, Please check the usage of different directories of the iOS file system.
You can refer this link: https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html
As per above link, apple has clearly mentioned that:
For Documents Directory : It clearly says that this directory should only contain files that you may wish to expose to the user.
If you want something that should not be exposed to User, then dont use Documents
directory, instead use Library
directory for this.
For Library
directory: Use the Library subdirectories for any files you don’t want exposed to the user. Your app should not use these directories for user data files.
Here, see the following screenshot from above link, which says about which directory to use when?
In short, keep your root directories content in Document directory and move all other directory with their content to Library directory OR Library/Application Support Directory. So, your intended purpose will get fulfilled.
Related Topics
How to Use Assets Catalog Color Sets
How to Get Monday'S Date of the Current Week in Swift
Swift Function Object Wrapper in Apple/Swift
What Sorting Algorithm Does Swift Implement For Its Standard Library
How to Declare a Variable That Has a Type and Implements a Protocol
Sheet Inside Foreach Doesn't Loop Over Items Swiftui
Using 'Self' in Class Extension Functions in Swift
What Is the Cause of This Type Error
How to Display My App Documents in the Files App For Iphone
How to Reset the App Between Tests in Swift Xctest Ui
Changing Navigation Title Programmatically
How to Make an Enum Decodable in Swift
Calling Protocol Default Implementation from Regular Method
Transparent Background For Modally Presented Viewcontroller
How Change Background Color If Using Navigationview in Swiftui
What Does "Fatal Error: Unexpectedly Found Nil While Unwrapping an Optional Value" Mean