Skip to content

Instantly share code, notes, and snippets.

@babldev
Last active August 31, 2023 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save babldev/1b86a91907611ec25675e42d3b3709bc to your computer and use it in GitHub Desktop.
Save babldev/1b86a91907611ec25675e42d3b3709bc to your computer and use it in GitHub Desktop.
Make iOS app files saved to Documents directory visible in the Files app

Documentation is very limited online, but apparently you need both Info.plist flags UIFileSharingEnabled and LSSupportsOpeningDocumentsInPlace to get the Documents directory visible in the Files app in the "On My iPhone" section.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>UIFileSharingEnabled</key>
<true/>
<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>
</dict>
</plist>
import Foundation
func writeTextToFile() {
// The text you want to write
let text = "Hello, world!"
// Getting the URL of the Documents directory
if let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
// Creating the full URL by appending the text file name
let fileURL = documentsDirectory.appendingPathComponent("MyTextFile.txt")
do {
// Writing the text to the file
try text.write(to: fileURL, atomically: true, encoding: String.Encoding.utf8)
print("Successfully wrote text to file at \(fileURL)")
} catch {
// Handle the error
print("An error occurred: \(error)")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment