xcode/ios: trying to copy files to caches folder on install - iphone

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];

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.

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).

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

(IPHONE) Big confusion to read/write the same file from resource folder

i've read several thread for this question but unfortunatly not found the answer to my problem :(
i have a xml file in resource folder, and i need just to re-write the same file;
the app logic is:
display data loaded from file
add new data to the same file
for read a file my code is:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"myfile" ofType:#"xml"];
NSData *dataXml=[NSData dataWithContentsOfFile:filePath];
for re-write the same file:
NSString *myDataString = #"?xml version=""1.0"" encoding=""UTF-8""?";
NSString *path = [[NSBundle mainBundle] pathForResource:#"myfile" ofType:#"xml"];
BOOL ok=[myDataString writeToFile:path atomically:YES encoding:NSUnicodeStringEncoding error:nil];
For read works fine, but when i try to write, the file is not updated, but it was created another one in another folder, i've tested this in the simulator noting that an app is in a folder like this:
Users/user/Library/Application Support/iPhoneSimulator/Users/Applications/05279766-7B2C-4FC0-BFB0-D87A158A3337/
where there are 3 folder (Documents,tmp,Libary) and the bundle file .app (where there is my xml file)
My question are: is my logic wrong? in wich way are managed app folders in the device??
thanks in advance and sorry for the long thread!
You cannot write files into your application bundle; it's read only (on the device, at least; you may be able to do this in the Simulator).
What you should do is use your ~/Documents folder for the file. If it's not there, first read it from your bundle, then write it out to ~/Documents, and from then on, read it from there.

How do I get an NSArray of filenames of all files in a given directory in my app?

What I want to do seems simple enough: Get an array of filenames in a given "directory" on my app. But the more I play around with NSFileManager and NSBundle I find myself getting more lost...I just want to get the filenames of the files organized in a specific directory in my iPhone Xcode project...For example: The directory in question is called "Images" (created using "Add > New Group" under the project icon in Xcode) and contains 10 .png images. It seems simple but I am getting nowhere. I'd appreciate some help in finding the answer.
Q: How do I get an NSArray of filenames of all files in a given directory in my app?
If you want the folder to be part of the App bundle, you need to create a folder reference inside the Resources group, instead of a group (it will show up in Xcode as a blue folder as opposed to the yellow one).
To do this, you drag the folder from Finder and select folder reference when prompted.
Once you have that, you should be able to get the contents of the folder with something like:
NSError *error = nil;
NSString *yourFolderPath = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:#"YourFolder"];
NSArray *yourFolderContents = [[NSFileManager defaultManager]
contentsOfDirectoryAtPath:yourFolderPath error:&error];
The accepted answer's been deprecated for years now. The following should be used instead.
- (NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error
Update: #macserv updated the accepted answer after I posted this.
Nowadays, this is how you do it:
From the Finder, Drag & Drop your folder in your project's navigator and the next popup will show up:
As you can see in that screenshot, you have to select Create folder references.
Do what the solution for this post is tells you to do. I mean:
NSError *error = nil;
NSString *yourFolderPath = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:#"YourFolder"];
NSArray *yourFolderContents = [[NSFileManager defaultManager]
contentsOfDirectoryAtPath:yourFolderPath error:&error];