1

I made a CSV file, but it won't export, at least I can't find the files anywhere located on my phone, do i need to add some information to my plist? I don't know why it won't export to my phone.

@IBAction func export(_ sender: Any) {
    var counter = 0;
    var csvString = "\("latitude"),\("longitude")\n\n"
    while coordinatesLat.count != counter{
        csvString = csvString.appending("\(String(describing: coordinatesLat[counter])) ,\(String(describing: coordinatesLong[counter]))\n")
        counter+=1
           }
           print(csvString)
           let fileManager = FileManager.default
           do {
               let path = try fileManager.url(for: .documentDirectory, in: .allDomainsMask, appropriateFor: nil, create: false)
               fileURL = path.appendingPathComponent("leggo.csv")
               print(fileURL)
               try csvString.write(to: fileURL, atomically: true, encoding: .utf8)
               
           } catch {
               print("error creating file")
           }
    
}

this is the print(fileUrl):

Optional(file:///var/mobile/Containers/Data/Application/8517AA9C-4C25-46A8-BE30-15BCEA46E255/Documents/leggo.csv)

Jordy
  • 37
  • 6
  • 1
    What makes you think that it isn't exporting? Does it print the "error creating file" message? Does it get to `print(fileURL)`? – jnpdx Mar 08 '21 at 08:13
  • 1
    `print("error creating file")` => `print("error creating file: \(error)")`, get the error thrown if there is one. But start by defining "won't export to my phone. – Larme Mar 08 '21 at 08:14
  • I can't find the file on my phone, that's the reason, I do not get any error messages – Jordy Mar 08 '21 at 08:21
  • 1
    How do you look for the file? The usual way to locate it, is to connect your device to the mac, start "Finder", search on left side for your device, open it by clicking on it, look for the "files" tab, open it, look for your app, expand it .. and you should see the documents folder of your app. ... and the files .. your code above looks good – Hardy_Germany Mar 08 '21 at 08:42
  • this is a good article which explains the several possibilities to store data in files on an iPhone. https://medium.com/@anandin02/ios-storage-best-practices-294fca83ad9 – Hardy_Germany Mar 08 '21 at 08:49
  • Hey! thank you, I cannot find my app in the files tab. – Jordy Mar 08 '21 at 08:53
  • I found my error, I didn't turn file sharing on in my plist, so thank you for the article Hardy! – Jordy Mar 08 '21 at 08:56
  • Although this is for react-native, the underlying code is for native iOS, and setting these two variables on your Info.plist will allow you to access the documents directory of your app. [React native fs iOS not showing files in the document directory](https://stackoverflow.com/questions/54626359/react-native-fs-ios-not-showing-files-in-the-document-directory) – Andrew Mar 08 '21 at 08:57

0 Answers0