accessing plists on iphone simulator - iphone

So I have started building plist files using NSCoder, but as I"m developing i want to look at them and see what data is being saved.
How do i find/access the plist files generated by my app in the simulator (and on the device when i get to that)
Places Ive looked:
application bundle
build directory

To put them into the ~/Library/Application Support/iPhone Simulator/User/Applications/ subfolder (where they would end up if they were on a real device), you could use the following piece of code in your application:
+ (NSString *)dataFilePath:(NSString *)filename {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:filename];
}
This will put the file in the /Documents/ folder of your application's sandbox.
You can then find the file back at: ~/Library/Application Support/iPhone Simulator/User/Applications/ Application GUID /Documents/
Good luck.

wow that is dumb,
found them at /
this simulator must not emulate the real phone environment well at all.

If your plist is in the Document folder then give the path as
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
else if your plist file is in the resource folder then give the path of resource folder as
[[NSBundle mainbundle] pathforresources:..........];

Related

how to create plist file in xcode 4.3.2

I am trying to create my own plist file to be used in my application, I am using apple documents as refrence for doing this.
However, I have read that if you create your own plist you need to place it in the resource folder in your applications build. The issue with that is that I have no resources folder in my build... so I am woundering what should I do?.. I have read this guys answere here, he says its fine to just place the plist file in the supporting Files folder.. is this okay to do in regards to allowing the plist to be read and written too?
To read and write to plist the best practice is to copy it to document root if you want to access it through bundle you don't have write permission. I have provided a snap shot of the code here and how you can accomplish this.
NSError *err;
NSFileManager *fileManager = [NSFileManager defaultManager];
//getting the path to document directory for the file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir = [paths objectAtIndex:0];
NSString *path = [documentDir stringByAppendingPathComponent:#"YourFile.plist"];
//checking to see of the file already exist
if(![fileManager fileExistsAtPath:path])
{
//if doesnt exist get the the file path from bindle
NSString *correctPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"YourFile.plist"];
//copy the file from bundle to document dir
[fileManager copyItemAtPath:correctPath toPath:path error:&err];
}
You can put that plist file anywhere you want.
The important thing will be copying it into the bundle. So to be sure for that check
project settings>build phases>copy bundle resources
You can open project settings by left-clicking on your project in the project navigator.

Xcode: directory file for simulator Iphone

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
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
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!
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
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.
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.
Paste this in the terminal.
open ~/Library/Application\ Support/iPhone\ Simulator/
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]
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

Where to put text files for iPhone UITextView

I have multiple UITextViews with corresponding .txt files. I'm reading them with NSString's
stringWithContentsOfFile
but I don't know the path where I should put my files. If I put it to /tmp/ on my Mac, it works in Simulator, but, of course, doesn't work on the actual device. So where should I put the files, so they'll work on both Simulator and actual Device.
Add them as resources to your project and you'll be able to load them from your application bundle using path:
NSString* filePath = [[NSBundle mainBundle] pathForResource:#"filename"
ofType:#"txt"];
If you want to make these files editable, though, you can keep them in Documents folder in application sandbox. To get its path you can use:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
So workflow in this case may be the following:
Check if required file is present in documents folder. If no - copy it there from resources.
Read data from file (from Documents folder)
Save updated data to file (in Documents folder) if needed
Place them into Documents folder under your application structure. You can get actual path to it by calling:
NSString *docsDirPath = [NSHomeDirectory() stringByAppendingPathComponent: #"Documents"];
This will work on the device as well as in the Simulator.
You'll want either NSTemporaryDirectory or NSDocumentDirectory.
An in-depth example for using UITextView with files can be found at http://servin.com/iphone/iPhone-File-IO.html

iPhone writeToFile doesn't work

I have written one application that save txt file into device. When I test on simulator, I am able to read and write file. After I test on device, read is working fine but it doesn't write anything on that text file. I have searched for the solution but it seems like it has problem with right access. Anyone can suggest me how to make this work?
What path are you using to save the file to?
I'm using
- (NSString *)dataFilePath{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:#"myFileNameHere.txt"];
}
and then pass [self dataFilePath]; into the writeToFile method as the path
On real device you don't have access to write everywhere. See "Commonly Used Directories" in Developers Guide - you should write to Documents or Caches directories (how to get their paths)

Where to keep .plist file in iphone app

I am developing an iPhone app, in which i want use an .plist file to save some config variables.
in my xcode where to create that .plist file and how to access it???
thank you,
You would put in the resources folder. Then use something like this to load it:
NSString *file = [[NSBundle mainBundle] pathForResource:#"TwitterUsers" ofType:#"plist"];
NSArray *Props = [[NSArray alloc] initWithContentsOfFile:file];
Where TwitterUsers is the name of your plist file.
If your plist file contains keys and values you would make the second line an NSDictionary instead of NSArray.
To store the plist file in your Documents directory you will have to include plist file in your app and then on first launch copy it to Documents directory.
To get access to the file in Documents directory:
NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
// <Application Home>/Documents/foo.plist
NSString *fooPath =
[documentsPath stringByAppendingPathComponent:#“foo.plist”];
I would recommend keeping it in the Resources directory in the app bundle, but you can just drag it into the project window. The NSBundle method pathForResource:ofType: should give you a path, which you can pass to NSDictionary's dictionaryWithContentsOfFile:.
Edit: Sorry, full code sample (thought I'd already copied & pasted):
NSString *path = [[NSBundle mainBundle] pathForResource:#"MyConfig" ofType:#"plist"];
NSDictionary *myConfig = [NSDictionary dictionaryWithContentsOfFile:path];
EDIT Just occured to me "config variables" might not be immutable. If that is the case, disregard this answer.
I would recommend you use NSUserDefaults to store configuration variables instead of rolling your own system.
If you are modifying a .plist inside your application bundle, you will risk invalidating the signature on the bundle, while will not end well.
If you must store and modify your own .plist, store it in the Documents directory, which is also where your application should store anything that it downloads, creates, etc. See File and Data Management and NSFileManager.
By default if you have included the plist in the project anywhere (under Resources or otherwise) XCode will copy it into the application bundle where you can get to it with the aforementioned pathForResource call. Just thought I'd mention that as you might prefer a grouping where you do not have it in resources...
you can find the example sqlite book ..... the silte data file was save into the directory 'Document' .... you will know the prcess
Why would it invalidate the bundle?
Would this plist file in the resources folder be backed up in iTunes like files in the documents folder?