24

i want to write a string in a file "file.txt": this file in my project (for Iphone) is inside Resources; I try to write a string in this file but it don't work, I show my code.

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath =  [documentsDirectory stringByAppendingPathComponent:@"file.txt"];
NSError *error;
[outputString writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];

I want write in this file with simulator xcode, not with device

cyclingIsBetter
  • 16,737
  • 47
  • 149
  • 236

8 Answers8

49

I'm not sure if you are able to write in your bundle, but you can in your Documents directory instead as your code does. Why don't you try this?

Use the same code and you will find your file in:

/Users/YOURUSER/Library/Application Support/iPhone Simulator/IOSVERSION/Applications/APPID/Documents/file.txt

Jorge
  • 2,046
  • 1
  • 16
  • 22
19

This changed with XCode 6 and iOS 6 and this got tricky.

The documents directory are now hidden within:

~/Library/Developer/CoreSimulator/Devices/<some-id>/data/Containers/Data/Application/<some-other-id>

It seems the location is changing at each application start, so it's really hard to track. There is a simple tip I borrowed from this article and that I will include for the lazy ones:

#if TARGET_IPHONE_SIMULATOR
// where are you?
NSLog(@"Documents Directory: %@", [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]);
#endif

Add this for example to your applicationDidFinishLaunching method, should make it easier!

If you want more details, just have a look to the original article

NB: I know the question is old, but well, I found it while searching, so I think adding an updated answer may serve some of us!

Romain Champourlier
  • 2,320
  • 24
  • 29
15

The Documents directory for your app in the simulator is located at;

~/Library/Application Support/iPhone Simulator/YOUR-IOS-VERSION/Applications/UNIQUE-KEY-FOR-YOUR-APP/Documents
Matthew Frederick
  • 22,017
  • 10
  • 68
  • 96
8

Stop at a breakpoint anywhere in your app and type "po NSHomeDirectory()" in the debugger. Use Finder's Go > Go To Folder feature to jump directly there. how the "po NSHomeDirectory()" command in debugger shows path to current app's home directory

Vitalii
  • 3,586
  • 29
  • 40
2

With the example code that you have here it will not write into your Resources folder, but into the Documents folder of the Simulator, which is actually where you should write on device, but as you mention that you only want to do this on the simulator, you could use

filePath = [[NSBundle mainBundle] bundlePath] stringByAppendingPathComnponent:@"Resources"]

But, don't do this in a shipping app, it will fail.

Scott Little
  • 1,857
  • 15
  • 15
1

Paste this in the terminal.

open ~/Library/Application\ Support/iPhone\ Simulator/
Robert
  • 35,442
  • 34
  • 158
  • 205
0

In gdb, stop process and paste it, then the location of Documents will be printed. [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]

 po [[[NSFileManager defaultManager] URLsForDirectory:9 inDomains:1] lastObject]
0

If you are using Xcode 7 and higher follow this:

Documents Directory:

file:///Users/codercat/Library/Developer/CoreSimulator/Devices/YourProjectDict/data/Containers/Data/Application/(sample digit)7F53CFB3-C534-443F-B595-62619BA808F2/Documents/your file located here
codercat
  • 21,439
  • 9
  • 56
  • 84