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.
Related
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
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [[pathArray objectAtIndex:0] stringByAppendingPathComponent:#"data.plist"];
and
return [[NSBundle mainBundle] pathForResource:#"data" ofType:#"plist"];
the file is stored in my documents folder.
No, they are not. The former returns a path to the file named data.plist in the app's Documents directory, the latter returns a path to the file named data.plist in the app's bundle, where all the app's resources, executable, etc are located.
From the documentation for NSSearchPathForDirectoriesInDomains
Creates a list of path strings for the
specified directories in the specified
domains. The list is in the order in
which you should search the
directories. If expandTilde is YES,
tildes are expanded as described in
stringByExpandingTildeInPath.
From the documentation for the NSBundle method pathForResource:ofType:
The method first looks for a matching
resource file in the non-localized
resource directory of the specified
bundle. (In Mac OS X, this directory
is typically called Resources but in
iOS, it is the main bundle directory.)
If a matching resource file is not
found, it then looks in the top level
of any available language-specific
“.lproj” directories. (The search
order for the language-specific
directories corresponds to the user’s
preferences.) It does not recurse
through other subdirectories at any of
these locations.
Therefore the former looks for files in a directory, and the latter looks in the bundle. These may not coincide.
I've read up Apple's documentation of plist: http://developer.apple.com/library/mac/documentation/cocoa/Conceptual/PropertyLists/PropertyLists.pdf
However I've got a few questions about it:
1) When we use the [dict writeToFile:plistPath atomically:YES] API, does it overwrite the current content of the plist? It doesn't say anything in the documentation.
2) Are we supposed to actually make the plist manually in Xcode by new file->resources->property list? Or are we supposed to have this:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSData *xmlData = [NSPropertyListSerialization //... a very long line here
if([fileManager fileExistsAtPath:plistPath]) {
[xmlData writeToFile:plistPath atomically:YES];
}
else {
[fileManager createFileAtPath:plistPath contents:xmlData attributes:nil];
}
3) How do we check if we've actually written data to property list? I tried products -> myapp.app -> "reveal in finder" -> right click -> show package contents, and there are some plists there, but I can't see the data being written! Is that mean I'm failed writing data to plist?
EDIT: Thanks everyone! Sorry for being silly today!
From the description of writeToFile:atomically:
If flag is YES, the dictionary is written to an auxiliary file, and then the auxiliary file is renamed to path. If flag is NO, the dictionary is written directly to path. The YES option guarantees that path, if it exists at all, won’t be corrupted even if the system should crash during writing.
Since it is written to an auxiliary file and then renamed to the specified path, I would assume that it overwrites the current content of the file.
You should use the NSFileManager to find the application's documents directory and write the plist there. You should not use a resource, as you are going to write to it during the course of executing the app.
Add some logging (e.g., NSLog(#"plist path: %#", plistPath);) to show where the plist is getting written.
1) It will overwrite the current content. If you want to append some more data to your current plist file, first read it to a dictionary and add the data and write it back.
2) You can add it manually or create it programmatically.
3) Just log the contents of the plist file after writing.
I'm curious if there's a way to search for files in the iPhone's directory ( the location is irrelevant).
I am wanting to load in addresses from text files. The thing is additional files may be added and I want to dynamically be able to find the files and load in the data without hardcoding the file names to load in.
Thanks a bunch!
You want:
NSError *error;
[[NSFileManager defaultManager] contentsOfDirectoryAtPath:myPath error:&error]
It returns an array of NSString objects for each file in the given directory.
When installing an App to the iPhone, is there a way to install files to the App's Documents folder?
To say it another way: When the user downloads an App and installs it on their iPhone, I want to automatically install some files to the App's Documents directory.
For example: I have a file foo.txt that I create at development time for my App SeeFooRun. When the App installs, I want foo.txt to appear in the Documents directory so that when I run the App for the first time I access foo.txt from the Documents directory instead of from the App Bundle.
Thanks!
In the example I said "runs for the first time" when I meant to say "installs" and I changed the rest of the sentence to fit. Sorry for the mix up!
I would do it using this method:
Create a property in the root plist set the initial value to "NO" When the app is run, check this value.
If the value is "NO", create the document in the document directory, change the value to "YES" and save the value.
The next time the app is run, the value will be "YES" and the file won't be rebuilt.
Hope this helps.....
You can do something like this:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *pathLocal = [documentsDir stringByAppendingPathComponent:#"foo.txt"];
NSString *pathBundle = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"foo.txt"];
NSError *error;
BOOL success = [fileManager copyItemAtPath:pathBundle toPath:pathLocal error:&error];
One slight variation on the copying into Documents answers - put everything you copy into a folder in Documents that you create, then at startup you simply check for the presence of that one folder and do all the copying then.
Checking per file is more robust though, especially if you add a new file in an update then the copy logic will copy in the single new file without overwriting the older ones.