I am unknown of file manager in iphone
Can anyone explain the concept of file manager here
thank you
Quoting from the Apple Low-Level File Management Programming Guide
You use an NSFileManager object to
perform many generic file-system
operations—for example you can:
Create directories and files.
Extract
the contents of files (as NSData
objects)
Change your current working location in the file system.
Copy, move, and link files and directories.
Remove files, links, and directories.
...
An important point to understand is that your application runs in a "sandbox" - it has access only to its own files, not files created by other applications.
The section on the file system in the iOS Application Programming Guide describes the layout of your application and any files it creates.
For example, here's how you might use NSFileManager to open a file named "Defaults.plist" in your application bundle and read it into an NSData object:
NSString *plistPath = [[NSBundle mainBundle] pathForResource:#"Defaults" ofType:#"plist"];
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
Related
I'm using iPhone6 simulator, I'm trying to get a file (any extension pdf of html) by using any of this codes:
NSString *file = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"%#.pdf", documentName] ofType:nil];
or
NSString *file2 = [[NSBundle mainBundle] pathForResource:documentName ofType:#"pdf"];
I'm sure that the file in exists in Resources folder and I'm not add the file, I download it from web programmatically and see it in this path
( /Users/myMac/Library/Application Support/iPhone Simulator/6.9/Applications/E60F22DD-7301-48EF-AB25-B9D42FA6AD49/myApp.app) I see the file, but the code does not see it
but this codes sometimes gives me null and some other times gives me the file.
Why is this happen and how to prevent this and make it always gives me the file.
consider that I uses
if(file != nil)
and want to make if file == nil try to open it by using any way.
Thanks in Advance.
Make sure your file is added to project's target. Choose your target then Build Phases->Copy Bundle Resources. The file should be there.
Sometimes it's worth cleaning your project and building from scratch. I have seen cases when file added wasn't copied to simulator/device until clean was performed.
a) If you download anything from the web then the content goes to the document directory of your application.
b) No other application can interact with the your application doc directory.
c) The Bundle path i.e. the groups and files section of your application is read only.
d) No one can sneak your applications PDF unless and until you have allowed access to the document directory of your application.
Hope this helps
My app just got rejected by Apple with the following reason:
2.23
We found that your app does not follow the iOS Data Storage
Guidelines, which is required per the App Store Review Guidelines.
In particular, we found that on launch and/or content download, your
app stores 10.3 MB. To check how much data your app is storing:
Install and launch your app
Go to Settings > iCloud > Storage & Backup > Manage Storage
If necessary, tap "Show all apps"
Check your app's storage
The iOS Data Storage Guidelines indicate that only content that the
user creates using your app, e.g., documents, new files, edits, etc.,
may be stored in the /Documents directory - and backed up by iCloud.
What I do is, I deliver the database in the resource folder with about 10 MB and copy that database to the library path on initial startup (see code below). When looking at my app settings after the startup within the device settings, it actually says, that the documents & data folder contain this 10 MB of data. The app does not need that database anymore when it is once installed, so I just tried to remove the DB from the resource folder, when the copy is done by using the removeItemAtPath. But there seems to be a permission issue with that.
Here the code I am using to populate the database at initial startup:
// Copy the database from the app resource, if it is not already existing in the library path
- (void) copyDatabaseIfNeeded {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *dbPath = [documentsDir stringByAppendingPathComponent:#"abiliator.sqlite3"];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if(!success) {
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"abiliator.sqlite3"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (!success) {
NSLog(#"Failed to create writable database file with message '%#'.", error);
}
else {
success = [fileManager removeItemAtPath:defaultDBPath error:&error];
if (!success) {
NSLog(#"Failed to remove the source database file with message '%#'.", error);
}
}
}
}
After researching for hours about that issue, I am actually pretty sure, that I am not doing ANYTHING wrong at all. The Library path is the location to store updateable files according to Apple documentation. Especially if the data is wanted to be backed up and hidden / not exposed to the user. Both is the case for my app, I want the data to be backed up and I don't want the user to see my database. So Library seems perfectly right. Only thing I could think of is the size of the database in the resource directory. I could reduce that by zipping it. But what are the limits? Neither the reviewers nor the documentation could tell me anything specific on that.
So what is actually wrong? And if the resource directory is not the right place to store my source database for initial setup, what other directory could I use in my project?
thanks a lot for any hint.
René
Got a reply from Apple mentioning that I am supposed to store the user data in a different database than the data I am delivering. Though I am not very keen on that, as it increases the code complexity unneccessary and the user data in my app can get larger than the one I deliver anyway, I would like to make sure, that I get the correct solution approach for that now.
What I intend to implement is 2 directories: /Library/UserDB and /Library/AppDB. The AppDB would contain the delivered DB and the UserDB would contain the user data only and I would flag the AppDB Dir as non-backup. Guess that would make Apple happy and get my app approved for that matter, am I right? Would appreciate any opinion about that approach before I start implementing.
So as a learning exercise, I am trying to make a simple file browser that interfaces with a file storage mechanism. (Think dropbox or box.net) I want to add a feature that would allow the user to flag a file for local storage so they could view it when they were not connected to the network. Is there an apple API that allows for something like that.
Perhaps there is a way to add the documents to the local bundle and then access the files that way at a later time? I haven't been able to find much documentation on this. Any insight, guidance or just general advice would be greatly appreciated. Thanks!
You can find the application's document directory thus:
/**
Returns the path to the application's Documents directory.
*/
- (NSString *)applicationDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
Then get the URL (i.e. pathname) of a file within that directory:
NSURL *fileURL = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent:theFilename]];
and use Cocoa file-writing methods (e.g. of NSString, NSData), or you can just use ordinary stdio and so on.
I have noticed that in the CoreDataBooks example a default database is copied to the documents directory if the file doesn't already exist there:
if (![fileManager fileExistsAtPath:storePath]) {
NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:#"CoreDataBooks" ofType:#"sqlite"];
if (defaultStorePath) {
[fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
}
}
My question is, does this double the space it takes up?
i.e. There are now 2 databases, one in the bundle one in the documents folder.
I have a much larger database and a whole bunch of images totaling to about 50mb. Is there another way to go about this without copying the data?
In the example this is done so there is a default file to write to. If this is not done, one would have to create the file in code. The reason for this is because the app bundle is in a sandbox where it is forbidden to write to.
If you need the database to be editable, you must move it outside the app bundle.
AFAIK, Apple strongly recommends against modifying files in the bundle. So if the data are read-only (like, most likely, your images), it's OK to keep it in the bundle. A mutable database is a whole another matter.
Is there a shortcut method much like NSHomeDirectory() and NSTemporaryDirectory to get to the resources folder within your project?
Is the /resources/ folder the best place to be storing a file?
You can't store writable data in your Resources path, since it's inside the app bundle (which is signed). But it's a good place to store readonly data. As Codezy mentions, [[NSBundle mainBundle] resourcePath] is the path to it, but generally the better way to access files in it is [[NSBundle mainBundle] pathForResource:ofType:] and its siblings (...:inDirectory:, etc.) The latter automatically handles localization when appropriate.
Yes, I store an SQL lite db in the resources folder. Here is a sample of how I get the path.
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:#"ratings.sqlite"];
I store things in the resources folder if I want each new version to overwrite it, else I store it in the documents folder.