12

I'm trying to show my data in my app in the Files app on my iPhone I searched a lot and did everything right, I don't know where the error is

func fileManger(nameFolder: String) {

    let manager = FileManager.default
    let DecomentFolder = manager.urls(for: .documentDirectory, in: .userDomainMask).last
    let Folder = DecomentFolder?.appendingPathComponent("\(nameFolder)")

    do {
        try manager.createDirectory(at: Folder!, withIntermediateDirectories: true, attributes: [:])
    } catch let error {
        print(error.localizedDescription)
    }
}

Also here I am sending the value to be a folder

@objc func alertNewFolder () {
    let alert = UIAlertController(title: "Create an album", message: "", preferredStyle: .alert)
    alert.addTextField(configurationHandler: nil)
    alert.textFields![0].placeholder = "name album"
    alert.textFields![0].textAlignment = .right
    alert.addAction(UIAlertAction(title: "cancel", style: .cancel, handler: nil))
    alert.addAction(UIAlertAction(title: "save", style: .default, handler: { ـ in
        if let textFileds = alert.textFields {
            let links = textFileds[0].text
            self.arrS1.append(addCatogrey(nameCatog: links!, imageSection: UIImage(named: "folder")))
// Here Send FileManager
            helperCoding().fileManger(nameFolder: links!)
            self.collection.reloadData()
        }
    }))
    self.present(alert, animated: true, completion: nil)
}

In the Simulators it saved in the Documents folder here correctly

/Users/badrshammry/Library/Developer/CoreSimulator/Devices/7986A27F-7026-45E1-9073-78CCD6A9B90A/data/Containers/Data/Application/3173C4DC-BCDE-41B9-89E1-6E8D9B52EF25/Documents
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
Badrshammry
  • 165
  • 1
  • 9

3 Answers3

23

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:

enter image description here

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • Is there a way to specify certain files that should not be displayed, or should i write those files somewhere else? – pir800 Mar 02 '23 at 18:28
  • 1
    if you don't want to expose certain files to the user you should write them to the app support directory. take a look at [File System Basics](https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html) docs – Leo Dabus Mar 02 '23 at 22:33
  • 1
    **Use this directory to store all app data files except those associated with the user’s documents. For example, you might use this directory to store app-created data files, configuration files, templates, or other fixed or modifiable resources that are managed by the app. An app might use this directory to store a modifiable copy of resources contained initially in the app’s bundle. A game might use this directory to store new levels purchased by the user and downloaded from a server.** – Leo Dabus Mar 02 '23 at 22:38
  • 1
    **All content in this directory should be placed in a custom subdirectory whose name is that of your app’s bundle identifier or your company. In iOS, the contents of this directory are backed up by iTunes and iCloud.** – Leo Dabus Mar 02 '23 at 22:38
  • 1
    Very much appreciated. – pir800 Mar 03 '23 at 23:11
  • Any other way to do it? There’s no plist on Swift Playgrounds projects and I need to stay in one for multitouch development. – oxygen May 16 '23 at 18:59
  • 1
    Thank you, I don't know why I needed to search so long for this simple answer! – Simon Henn May 31 '23 at 15:33
5

Add these two keys to 'info.plist' -

...
<key>UIFileSharingEnabled</key>
<true/>
<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>
...

After this, files saved in '.documentDirectory' will appear in 'Files App' inside a folder with your app name.

If editing 'info.plist' in Xcode then add below keys -

Application supports iTunes file sharing = YES
Supports opening documents in place = YES

info.plist

ReflectCode
  • 111
  • 2
  • 4
-1

To set Supports Document Browser has to be set in Custom iOS Target Propertiesin the info tab

info.plist is no more from Xcode 13

Anyhow, it does not work. The files App will not find files in the your app's bundle. They need to be "shared" (In SwiftUI use UIViewControllerRepresentable)

But your App will not have a directory in the file structure visible to the Files App. Adding that folder is still a mystery

See: How can folders visible to FIles app be created by App programatically

Wrog
  • 21
  • 5
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 19 '22 at 04:55
  • This is actually the correct answer! Set Supports Document Browser = YES in "Custom iOS Target Properties" for the target does work. Doing it in the info.plist directly does not work, was not working for me at all. As @Wrog points out, it must have changed after Xcode 13. – KeithTheBiped Jun 01 '23 at 18:46