2

I created a file in my ios app using below swift code.

let localFileName = String("file.rtf")
let text = String(“file contents”)

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 {}

    do {
        let text2 = try String(contentsOf: fileURL, encoding: .utf8)

        print("Read from file \(text2)")
    }
    catch {}


}

this code is working, returning true and contents of created file are being printed indicating that file is created successfully. Problem is that where to find that created file? Where is that file? Where to locate that file in iPhone without code?

Nil
  • 274
  • 3
  • 11
  • Use Xcode’s “Devices” to download the “Container”. See https://stackoverflow.com/a/38064225/1271826 – Rob May 18 '19 at 20:26

2 Answers2

0

Assuming you want to list files from document directory:

func listFiles() {
       let fileManager = FileManager.default
       let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
       do {
           guard let items = try fileManager.contents(atPath: path) else {
               print("no files found")
               return
           }
           for item in items {
               print("File: \(item)")
           }
       }
       catch {
           print("error")
       }
   }
Val Nolav
  • 908
  • 8
  • 19
  • 44
  • I was saying, where to find that saved file in iPhone, without code. – Nil May 18 '19 at 09:47
  • 1
    take a look at this: https://stackoverflow.com/a/25305782/784715 you can use iFunbox or iTunes to see those files – Val Nolav May 18 '19 at 10:20
0

Available directory in iOS

static let documents = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
static let library = try? FileManager.default.url(for: .libraryDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
static let downloads = try? FileManager.default.url(for: .downloadsDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
static let user = try? FileManager.default.url(for: .userDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
static let cache = try? FileManager.default.url(for: .cachesDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
ZeroOne
  • 584
  • 4
  • 17