简体   繁体   中英

open documents directory of my iOS app in Swift

In my iOS Swift App, I have created files in my App's documents directory through this code:

let localFileName = String("\(fileName).rtf")
let text = String("text text text")

if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {

    let fileURL = dir.appendingPathComponent(localFileName)
    do {
        try text.write(to: fileURL, atomically: false, encoding: .utf8)
    }
    catch {
    }

}

Now I want to Add a link in my app to this directory where file is created so that user can see files. Currently I am doing this by this code:

let importMenu = UIDocumentPickerViewController(documentTypes: [String(kUTTypeRTF)], in: .open)

        importMenu.delegate = self
        importMenu.modalPresentationStyle = .formSheet
        present(importMenu, animated: true, completion: nil)

but this code is for picking documents, not for opening directory, So How I can open my App's Documents directory, not for picking documents, just only for showing documents?

It is not possible to explore/open a directory in iOS app. Apple doesn't provide any api for the same. You need to create it by your own.

What you can do

You need to fetch all the files from the specific directory and list them all in either tableview or collection view.

And when user click on the any file, you can show that in web view or based on the file type you can do any specific operations.

So ultimately you need to explore more about FileManager .

This class contains what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM