Home>

Take the data (accrecord, girorecord) in the iphone app created with swift and put it in a .csv file
I want to output it and then save it. NSHomeDirectory () + "/ Documents" code
I don't know where it is stored in the ipone even if I look it up.

I also checked the file application that was included in the initial stage, but it did not seem to be saved.

What I tried

http://begigrammer.hatenablog.com/entry/2017/01/01/041039
I wrote the following code with reference to this page.

func createFile (fileName: String) {
     let filePath = NSHomeDirectory () + "/ Documents" + fileName + ".csv"
     print (filePath)
     print (accrecord)
     print (gyrorecord)
     do {
          try accrecord.write (toFile: filePath, atomically: true, encoding: String.Encoding.utf8)
          try gyrorecord.write (toFile: filePath, atomically: true, encoding:
               String.Encoding.utf8)
         print ("Success to Write the File")
     } catch _ as NSError {
          print ("Filure to Write File \ n \ (error)")
     }
Supplementary information (FW/tool version, etc.)

X-code12.0
Swift5

  • Answer # 1

    In order to make the file visible from the file application (filer), it seems that it is necessary to set two values ​​in Info.plist as shown in Step 1 of the URL below.

    How to view the Documents folder in the iOS 11 Files app and share it with other apps

    I tried to confirm that the saved data can be seen from the file app if both settings are made.

    Added on October 9, 2020

    For example, if you run the following sample, you can see that the file has been created.

    import UIKit
    class ViewController: UIViewController {
        override func viewDidLoad () {
            super.viewDidLoad ()
            // Do any additional setup after loading the view.
            createFile (fileName: "testdata")
            print ("file saved")
        }
        func createFile (fileName: String) {
            let documentPath = NSSearchPathForDirectoriesInDomains (.documentDirectory, .userDomainMask, true) .first!
            let filePath = documentPath + "/" + fileName + ".csv"
            let data = "1,2,3"
            do {
                try data.write (toFile: filePath, atomically: true, encoding: .utf8)
            } catch {
                print (error)
            }
        }
    }