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

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

Related

How to get the file name in iPhone sdk?

I stored a pdf file using NSData in my application memory. Now i want that file name each time to add those names into an array. How can i get the pdf file's name from my application memory to use in my app.
You can't since you only stored the file as a data object and not it's file name.
You could try to read the PDF meta data to check if there's a file name.
The answer here should help:
Getting a list of files in the Resources folder - iOS
It will list all the files in the Documents dir.
I suppose by "Application Memory" you mean one of the App's directories like Documents or Library.
You can just use NSFileManager to access the directory and get a list of all files, like if you stored it in your app's library folder:
NSArray* directoryPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUSerDomainMask, YES];
NSString* path = [directoryPaths objectAtIndex:0];
NSArray* files = [NSFileManager defaultManager] contentsOfDirectoryAtPath:path];
On top of that you can use a filter predicate to only get files with a .pdf ending.

iOS : NSString file path to NSURL

I have this :
NSURL *url = [NSURL fileURLWithPath:#"/Users/myusername/Library/Application Support/iPhone Simulator/4.2/Applications/5ABF1395-4A80-46C0-BD4A-419ED98CE367/Documents/DBV/v.m4v"];
Then I launch movieViewController but it always crashes.. This code doesn't work on the iPhone simulator neither on the device... How can I fix it ?
EDIT :
Before to write file path by hand, I used the correct way to select a folder.
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
Then i got this log:
file://localhost/Users/myusername/Library/Application%20Support/iPhone%20Simulat‌​or/4.2/Applications/5ABF1395-4A80-46C0-BD4A-419ED98CE367/Documents/DBV/v.m4v
Then I thought it was because of spaces in folder name , so i decided to write the full path by hand for debugging (replacing each %20 by space)
EDIT 2 : Notice : I'm trying to access a dynamically created file in Documents folder, not a file from my bundle.
For files in documents, you should be getting the path from NSSearchPathForDirectoriesInDomains.
//get list of document directories in sandbox
NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//get one and only document directory from that list
NSString *documentDirectory = [documentDirectories objectAtIndex: 0];
Then you append the file name to that.

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.

Writing xml file on iPhone (Linq=to=Xml and MonoTouch)?

I need to read, edit and save one xml file. In other words, I want to write again over the same file and not to create a new xml file. Reading and editing parts work, but not the saving.
The Linq-to-Xml code for saving is simply:
doc.Save(this.Path);
This works in iPhone Simulator but throws System.UnauthorizedAccessException on the device. The xml file is decorated as "content" in MonoDevelop.
Any help appreciated.
/pom
You need to save the file into one of the locations that you have write permissions on:
The /Document directory can be retrieved using:
var documents = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
Our "How To Store Files" page has more details:
http://wiki.monotouch.net/HowTo/Files/HowTo:_Store_Files
You are only allowed to save into your application's document directory. You can get it like this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
What is the value of this.Path?
var documents = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
var document = Directory.GetFiles(documents);

When saving something to disk with NSKeyedArchiver, where is that stored on the 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];