iOS: How to write to a file at a specific directory in the project?

b1skit

I'm trying to output some strings I've gathered in my app to a text file.

The text files will always exist in my project, like so:

I'm trying to overwrite whatever is in the file with a new (multi-lined) string:

func writeFile(){

let theFile: FileHandle? = FileHandle(forWritingAtPath: "./MyFiles/file.txt")

if theFile != nil { // This is always nil!!!
    let data = ("Some text\nline 2\nline3\n" as String).data(using: String.Encoding.utf8)

    // Write it to the file
    theFile?.write(data!)

    // Close the file
    theFile?.closeFile()
}
else {
    print("Writing failed.")
}

} // End file writing

However, the FileHandler always returns nil. I suspect that this is because I'm supplying the path and/or filename incorrectly?

I've been googling and stack overflowing for a couple of hours now... And still no idea of how to fix the problem. How do I specify the correct location and file?

Jitendra Singh

You cannot write file outside of the application sandbox. Each iOS app have individual directories Document, Library, tmp to store data.

func writeFile(){

    let documentsPath = NSSearchPathForDirectoriesInDomains(.documentationDirectory,.userDomainMask,true)
    let theFile: FileHandle? = FileHandle(forWritingAtPath: "\(documentsPath)/MyFiles/file.txt")

    if theFile != nil { // This is always nil!!!
        let data = ("Some text\nline 2\nline3\n" as String).data(using: String.Encoding.utf8)

        // Write it to the file
        theFile?.write(data!)

        // Close the file
        theFile?.closeFile()
    }
    else {
        print("Writing failed.")
    }

} // End file writing

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to write files to a specific directory?

From Dev

Write array to the plist file of my project directory

From Dev

Upload file to project specific directory dynamically in multer

From Dev

ios read csv file at project directory

From Dev

Firebase integrating into IOS project - No such file or directory

From Dev

How to write to a specific line of a file?

From Dev

How to write in file at specific location?

From Dev

How to find a specific file and move it to a specific directory?

From Dev

Batch file to write filenames from a directory into a text file with specific format

From Dev

how to write json file in ios

From Dev

How to access directory file outside django project?

From Dev

Qt: How to create a directory in project file?

From Java

How to tell Rubocop to ignore a specific directory or file

From Dev

How to create a text file inside a specific directory?

From Dev

How to Copy Only the Directories with a specific File in that directory

From Dev

How to create a text file inside a specific directory?

From Dev

How to set an alias for a specific file or directory?

From Dev

How to tar a file from the specific directory in linux?

From Dev

PHP How to write to a specific line in a file?

From Dev

How to write a specific data in file in JAVA

From Dev

How do you write specific bytes to a file?

From Dev

How to write specific line lengths of a file?

From Dev

Read file in project directory from native iOS code?

From Dev

How to Write a File to the Root Directory of a Zip File in Python

From Dev

How to write in a txt-file, iOS 7

From Dev

How to include C file in iOS project

From Dev

How to get directory of a folder inside xcode project? ios swift

From Dev

How to get directory of a folder inside xcode project? ios swift

From Dev

counting specific file in directory

Related Related

  1. 1

    How to write files to a specific directory?

  2. 2

    Write array to the plist file of my project directory

  3. 3

    Upload file to project specific directory dynamically in multer

  4. 4

    ios read csv file at project directory

  5. 5

    Firebase integrating into IOS project - No such file or directory

  6. 6

    How to write to a specific line of a file?

  7. 7

    How to write in file at specific location?

  8. 8

    How to find a specific file and move it to a specific directory?

  9. 9

    Batch file to write filenames from a directory into a text file with specific format

  10. 10

    how to write json file in ios

  11. 11

    How to access directory file outside django project?

  12. 12

    Qt: How to create a directory in project file?

  13. 13

    How to tell Rubocop to ignore a specific directory or file

  14. 14

    How to create a text file inside a specific directory?

  15. 15

    How to Copy Only the Directories with a specific File in that directory

  16. 16

    How to create a text file inside a specific directory?

  17. 17

    How to set an alias for a specific file or directory?

  18. 18

    How to tar a file from the specific directory in linux?

  19. 19

    PHP How to write to a specific line in a file?

  20. 20

    How to write a specific data in file in JAVA

  21. 21

    How do you write specific bytes to a file?

  22. 22

    How to write specific line lengths of a file?

  23. 23

    Read file in project directory from native iOS code?

  24. 24

    How to Write a File to the Root Directory of a Zip File in Python

  25. 25

    How to write in a txt-file, iOS 7

  26. 26

    How to include C file in iOS project

  27. 27

    How to get directory of a folder inside xcode project? ios swift

  28. 28

    How to get directory of a folder inside xcode project? ios swift

  29. 29

    counting specific file in directory

HotTag

Archive