-3

I am trying to read from a text file that I have already created on my desktop. I put this Story.txt into the corresponding file path on my computer through Xcode, something along these lines :

(/Users/username/Library/Developer/CoreSimulator/Devices/12F046A7-a lot more numbers/data/Containers/Data/Application/8AECCA06-160C-4702-B16E-FF50B2A145F5)

and the code works perfectly. However, when I try to run the app on my iPhone, it changes the file path and Xcode doesn't automatically transfer my files to my iPhone documents folder at this file path.

(/var/mobile/Containers/Data/Application/139C80A3-more numbers/Documents)

How do I manually put a .txt file at this location?

Here is the code I use to read the file.

func loadStory()
{
    if let dir : NSString = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true).first
    {
        let path = dir.stringByAppendingPathComponent("Story.txt")
        print(path)
        do
        {
            //reads file
            let text:String = try NSString(contentsOfFile: path, encoding: NSUTF8StringEncoding) as String

        }
        catch {/* error handling here */}
    }
}
pkamb
  • 33,281
  • 23
  • 160
  • 191
Brennan Adler
  • 895
  • 2
  • 8
  • 18
  • Just so it's clear, the whole point of using this text file is so that I don't have to add hundreds of lines of default text within my code. Keeps my code clean. I am never going to change the text file. I just want to read from it one time on app startup. – Brennan Adler Nov 28 '15 at 22:07
  • Do you want the file bundled with the app so when a user downloads your app, the file is already available to the app? – rmaddy Nov 28 '15 at 22:08
  • Exactly that, and I want it at the file path that the iPhone uses. – Brennan Adler Nov 28 '15 at 22:10
  • But for now I'm just trying to run the app on my iPhone through xCode. – Brennan Adler Nov 28 '15 at 22:10

3 Answers3

3

Since it seems you want the file included with your app, do not attempt to manually install the file somewhere on your computer. That only works with the Simulator if you happen to put the file in just the right place.

Instead, add the file to your Xcode project. Then the file will be added to your app's resource bundle. Then you can access the file using NSBundle to get its path. It will not be in the app's Documents folder. It will be in the app's bundle.

See Where to place a .txt file and read from it in a IOS project for more details.

Community
  • 1
  • 1
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Thank you, somehow I missed that thread. I was using the information from the "How to read/write from a text file" thread which used this strange file path. It works perfectly. – Brennan Adler Nov 28 '15 at 22:27
1

You need to add it to your project. Drag and drop it inside your project so it'll be bundled in. Than you can access it using

Let string = NSBundle.mainBundle().resourcePath as NSString
String.stringByAppendingPathComponent('Story.txt')
vale
  • 1,376
  • 11
  • 25
-1

If you could change the file from .txt to plist, you could integrate it on your project, and never think where it is, it will be always accessible.

FredericP
  • 1,119
  • 1
  • 14
  • 27