Badrshammry
Badrshammry

Reputation: 245

How can I display my App documents in the Files app for iPhone

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

Upvotes: 19

Views: 14073

Answers (4)

rBalmer
rBalmer

Reputation: 95

I am running iOS 17.0.1 and Xcode 15.2 and simple going into your project target, clicking on the info tab. and adding "Supports Documents Browser == YES" under Custom iOS Target Properties works perfectly fine!

If I go into the files App and click on "On My iPhone" there is now a folder for my app.

Please note, you may actually need to actually add a file for the folder to appear. For testing purposes, the following will do:

func saveToLocalDirectory() {
    let directory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
    let URL = StorageArchive.local.appendingPathComponent("testfile").appendingPathExtension("txt")
    let testString: String = "Test string to write in text file."
    do {
        try testString.write(to: URL, atomically: true, encoding: .utf8)
        print("Saving was a BIG success.")
    } catch {
        print("Saving Error: \(error)")
    }
}

Upvotes: 2

ReflectCode
ReflectCode

Reputation: 321

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

Upvotes: 13

Wrog
Wrog

Reputation: 61

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

Upvotes: 0

Leo Dabus
Leo Dabus

Reputation: 236340

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

Upvotes: 37

Related Questions