Xcode: directory file for simulator Iphone - 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

Related

XCode 4 - Easily access "Documents" folder of an app installed onto the simulator

When running an app onto the simulator, it creates a folder onto the Mac to contain the apps datas in a "Documents" folder. This folder is changed when you delete the app from the simulator, or when you change some app settings into XCode. When you have dozens of apps, it's a nightmare to find the good one by hand.
Is there a way to easily access this folder ?
Just gonna try answering. How about logging it, like this:
//App Directory & Directory Contents
NSString *appFolderPath = [[NSBundle mainBundle] resourcePath];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSLog(#"App Directory is: %#", appFolderPath);
NSLog(#"Directory Contents:\n%#", [fileManager directoryContentsAtPath: appFolderPath]);
What I do is create a link in the Finder sidebar to the simulator folder. It is approximately:
/Users/userName/Library/Application/Support/iPhone/Simulator/5.0/Applications/
Then I have that folder sorted by 'last modified'. Once I find the app folder for the app I am working on, I expand it to display the app (and see the app name) and also set a color on the folder. The Finder sometimes doesn't keep the folder sorted by 'last modified' unless the window is closed and reopened.
The location in general is:
/Users/yourusername/Library/Application Support/iPhone Simulator/7.1/Applications/appcode/Documents/yourdocuments.db
'yourusername': username you used for your development machine
'appcode': application identifier that is a series of numbers such as: 32F88907-B348-4764-9819-A5B6F9169FB7 (unique to your app)
the version number (7.1) in the path depends on the version of Xcode and hence the simulator.
directoryContentsAtPath: is deprecated.
Instead use contentsOfDirectoryAtPath:error:
//App Directory & Directory Contents
NSString *appFolderPath = [[NSBundle mainBundle] resourcePath];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSLog(#"App Directory is: %#", appFolderPath);
NSLog(#"Directory Contents:\n%#", [fileManager contentsOfDirectoryAtPath:appFolderPath error:nil]);
See https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/#//apple_ref/occ/instm/NSFileManager/directoryContentsAtPath:
Bazinga's answer (along with sebrenner's deprecation notice) is great for Objective-C.
For Swift, the equivalent would be:
let appFolderPath = NSBundle.mainBundle().resourcePath
let fileManager = NSFileManager.defaultManager()
print("App Directory is: \(appFolderPath)")
print("Directory Contents:\n \(fileManager.contentsAtPath(appFolderPath!))")

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/ios: trying to copy files to caches folder on install

I'm trying to copy a bunch of files to my app's caches folder when the app is installed on the device. I tried to use the Copy Files phase to achieve this, but i don't really know which path to use for its destination.
is this the right approach ... ?
or is this not possible at all?
I am not sure if it is impossible using this approach. However you can always write a method which preloads your resources on launch to Caches dir. Here's sample code:
NSString* path=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
path=[path stringByAppendingString:#"/plpart1.txt"];
NSError* error;
NSString* source=[[NSBundle mainBundle] pathForResource:#"pl_part1" ofType:#"txt"];
[[NSFileManager defaultManager] copyItemAtPath:source toPath:path error:&error];

iPhone:Accessing documents directory file path in Safari

I have placed a file programmatically in documents folder on iPhone like below.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSError *error;
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:#"myfilename.xxxxx"];
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"myfilename.xxxxx"];
BOOL success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {
NSAssert1(0, #”Failed to create writable database file with message ‘%#’.”, [error localizedDescription]);
}
I want to access this file in Safari by giving the path of local file through the below code.
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:localFileURL] ];
Is it possible? I saw somewhere, we can use the URL starting with "file:///" . Is it true? If yes, how can i access my documents directory file path in Safari passing the URL via the above one line code?
Please advise.
Thank you!
I would expect that that is not possible because every app is sandboxed, which means your app's data is not accessible to any other app, and vice versa.
To add to that, file:// is not one of the recognized or supported iOS schemes, as far as I know.
See run locally created file on iPhone safari
If what you mean is to run the html in a browser inside the app that LOOKS LIKE Safari, you can use a UIWebView and call
loadHTMLString:baseURL
You can create your file URL like this:
NSURL *myFileURL = [NSURL fileURLWithPath: pathToMyFile];
But like everyone else says, you can't access an app's file from any other app (without jailbreaking).

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