When saving something to disk with NSKeyedArchiver, where is that stored on the iPhone? - iphone

Example: If I used this, where does the iPhone store the file?
if (![NSKeyedArchiver archiveRootObject:your_object toFile:#"filename.plist"])
// save failed.
On my mac the filename.plist goes to Macintosh HD directly. No folder. Also, the NSKeyedArchiver doesn't seem to ask for a path, just for a file name. Strange or not?
And how's that file backed up with iTunes?

Well that actually depends on where you ask it to save the file, in your case, you're telling
it to store filename.plist, which will be stored in your root / .
if you want to store it in a specific location , just give it the full path, something like what Diederik Hoogenboom told you (this will store it in the Documents dir)
OR just give it the absolute path you want to save the file to.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent: #"myFile.plist"];
[NSKeyedArchiver archiveRootObject:your_object toFile:filePath];
This should probably have cleared it up a little for you.

archiveRootObject:toFile: (has been deprecated).
So you should pass it a path instead of just a filename. Otherwise NSKeyedArchiver will assume you want to store it in the root of the devices hard disk.
Update:
With the data result generated with NSKeyedArchiver, use the Data.write(to:options:) instance method which writes the data object's bytes to the location specified by a given URL.

The proper location to store the files is the documents folder:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

Related

Which file format should I use to save an array to a file?

I am using the solution found here: Save NSArray to File and I am just wondering what type of file format I should save my array as. Can I just use .txt?
Any format. iOS writeToPath: does not read the extension. You can even make up weird file names.
Take the codes from your reference question as example,
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:FILE_NAME];
[myArray writeToFile:filePath atomically:YES];
the FILE_NAME could be in any ASCII values.
The writeToFile of an array object creates a property list file. So, while you can use whatever extension you want, it's generally good style to use the plist extension.

How to store files on iOS in diffrent temp/catch folder

I'm trying to download messages from server and storing it on iPhoine Temp/catch folder like this :
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cacheDirectory = [paths objectAtIndex:0];
return cacheDirectory;
But at the same time i.m having issues when messages get unzipped it overriding same name images in the data base and displays same images ion all messages
My question is how to store each FILE IN its own folder inside iOS temp/catch file system so that same name images don't get override.
Append a directory to your cacheDirectory:
[cacheDirector stringByAppendingPathComponent:#"directoryName"];
You can store individual e-mails inside. I also hash the file URLs as the filename.
append to cacheDirectory the "/filename_dir" them append the filename and store there.
Hope it helps

About NSSearchPathForDirectoriesInDomains

I hv spent quite a while but no luck on following question:
I tried to access Document directory.
Both following codes work perfectly. Could someone tell me whats the difference between
last object and objectAtIndex:0
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [searchPaths lastObject];
or
NSString *documentPath = [searchPaths objectAtIndex:0];
The most close question was How to get URL for application's document directory iPhone, but it didn't explain the difference between lastObject and objectAtIndex:0.
I have read thru Apple Developer Library. It only says 'The directory returned by this method may not exist. This method simply gives you the appropriate location for the requested directory. Depending on the application’s needs, it may be up to the developer to create the appropriate directory and any in between.' Someone please help, thx.
In this case, there is no difference between [searchPaths lastObject] and [searchPaths objectAtIndex:0], because searchPaths contains only one entry. The user domain only contains one document directory.
If you try this you'll get different results:
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(
NSApplicationDirectory, NSAllDomainsMask, YES);
searchPaths may contain more than one directory if you look with NSAllDomainsMask.
Since Xcode 5, best practice is:
NSString *documentPath = [searchPaths firstObject];

iphone writeToFile: problem

Is there any way to save my .xml file to another directory other than "/Users/student/Library/Application Support/iPhone/Simulator/User/Applications/..."
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:#"accUserNames.xml"];
BOOL ok = [content writeToFile:appFile atomically:YES encoding:NSUnicodeStringEncoding error:nil];
if (!ok) {
NSLog(#"Error writing file !");
}
i wish to writeToFile: my .xml file to the desktop , any idea on how?
May the below code help,
NSString *documentsDirectory = #"/Users/student/Desktop/";
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:#"accUserNames.xml"];
BOOL ok = [content writeToFile:appFile atomically:YES encoding:NSUnicodeStringEncoding error:nil];
if (!ok) {
NSLog(#"Error writing file !");
}
From within the iPhone Simulator, you should be able to successfully use #"/Users/student/Desktop/accUserNames.xml" as the path to write to. However, you can't do this on an iOS device (you'll be restricted to the application's sandbox directory — it's recommended you write to the Documents folder or other folders in there, depending what type of data you're storing).
Edit: I think I understand your problem. This part of your code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
effectively finds the path to "/Users/student/Library/Application Support/iPhone/Simulator/User/Applications/..." (this is the normal place you want to save things). But if you just want to save to the desktop temporarily, you should just use appFile = #"/Users/student/Desktop/accUserNames.xml".
Note: I don't advocate this as a long-term solution, but if you just want to see the output of your program temporarily, it works fine.
If you want to upload your file to a server ("www.blahblah.com" in your example), then using the write to file methods is not the correct approach. This is only for writing data to the local file system (or on a network share, but that doesn't apply to iPhones).
If you want to transfer data to a webserver, you will need to have something on the server that will listen for a connection request, then it will need to accept the data which is transferred from your app. You cannot just write a file to "www.blahblah.com" as you would to a file system
Probably not with code, but you could try this: Open "Automator" (in the Utilities folder) and chose "Folder Action". As Input folder, you specify the directory of the documents (/Users/student/Library/Application Support/iPhone/Simulator/User/Applications/...) and then you select, from "Files & Folders", "Duplicate Finder Items" and "Move Finder Items" and select the Desktop. Hit "Save", give it a name, and all files in the documents folder should be copied to the desktop.

How to grab property list file created in Xcode?

I am having a hard time figuring out how to get the property list file that was created using Xcode. I created a property list file using array with NSString members. I want to grab that file and get all the NSString members and save it to a UITextField. But my problem is that I can't see that file. I don't know if I'm looking in the wrong path, or I don't know where the property file is saved.
Most likely, if the file was added via Xcode, the file is in your bundle. To open it use:
NSDictionary *plist = [NSDictionary arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"NameOfFile" ofType:#"plist"]];
Run this in the simulator and then look in the Finder, in Library/Application Support/iPhone Simulator/Applications/... to see if you can find the file, open it in and double check it to make sure Xcode added the file.
To make a copy in your Documents folder:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"My.plist"];
if (![plist writeToFile:path atomically:YES])
NSLog(#"not successful in writing the high scores");