Get file path from Document directory - Objective-c iOS - ios

Actually, I'm relying on full file path for security options (for that I need file path for it to be stored in keychain). It means that when I rebuild the app (or when it will get an update when published to AppStore), these features are broken due to bundle ID change in file path.
I've thought of a solution for this issue : get file path only from Documents directory (/Documents/.../.../myFile.pdf) rather than full path (/var/mobile/.../...). Is there a way of doing this ?
Alternatively is there any other solution to my issue ?
Thanks in advance !

Get the File Path from Document Directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *getPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#",strFileName]];

Thanks to #Larme and #user3182143, I've found a solution : getting Documents path and splitting file path by 2 : one part before Document directory
bundlePath = [documentsDirectory stringByDeletingLastPathComponent];
and the other part with
documentsPath = [documentsDirectory lastPathComponent];
...
filepath = [documentsPath stringByApendingPathComponent:#"myFile.pdf"];
If I need the full path, I'll get it by
fullPath = [bundlePath stringByAppendingPathComponent:filepath];

Related

my app creates the /var/mobile/ folder but I can't find it in Finder

My Xcode app creates a document and resource path. Please see the console output below when I print out these two path variables:
However, when I try to find this folder in Finder, I'm told the folder doesn't exist.
Why?!
The code for getting the two paths are below:
// Get the path to the resource directory.
NSString* resourcePath= [[NSBundle mainBundle] resourcePath];
std::string *resourcePathCPP = new std::string([resourcePath UTF8String]);
// Get the path to the Documents direcotory
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsPath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
std::string *documentsPathCPP = new std::string([documentsPath UTF8String]);
Thanks!
That should be on your iPhone/iPad's file system, not on your Mac. Otherwise Go to folder ... Is the right way to check a directory.

Using directory to store information and keep this information in a App Update

I have an app that stores images from the user in the app/documents folder, I'm doing the following code determine the path to save it:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docs = [paths objectAtIndex:0];
NSString *imageFilename = [[[[NSNumber alloc] initWithInteger:self.nextID] stringValue] stringByAppendingFormat:#".jpg"];
NSString *dirPath = [docs stringByAppendingFormat:#"/Photos/"];
imagePath = [dirPath stringByAppendingString:imageFilename];
it gives me a path similar to (in simulator):
/var/mobile/Applications/C9C43CFD-6A5F-40CF-8BAE-20496B5A544A/Documents/Photos/1.jpg
I save this path to show this image when I need.
My problem is, when I sent an update in the app store, after update the app it cant show the images anymore.
My understand is the Documents folder cant change when update the app in AppStore.
My probably reason for that it the name between Applications folder and Documentar has changed, is it true when update a app version from AppStore?
Does anyone has any idea? is there any way to test this app update local?
Thanks
You need to save the relative path after the Documents directory, not the absolute path. The app's directory can change during an update but the structure inside the app's sandbox won't change.
Also, there is a slightly better way to build your path:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docs = [paths objectAtIndex:0];
NSString *imageFilename = [NSString stringWithFormat:#"%d.jpg", self.nextID];
NSString *dirPath = [docs stringByAppendingPathComponent:#"Photos"];
imagePath = [dirPath stringByAppendingPathComponent:imageFilename];
When the app is updated from the App Store, the contents of the Documents directory are unchanged. The app bundle and all its contents are replaced with the new version, but the Documents folder is a safe place to store content between app versions.

iOS zip subdirectory documents folder

I'm using ZipKit for iOS I'm trying to zip a subdirectory of my Documents folder. I've referred to this question but it still doesn't seem to be working. My code is:
ZKFileArchive *archive = [ZKFileArchive archiveWithArchivePath:#"Documents/test.zip"];
NSInteger result = [archive deflateDirectory:#"/Documents/test" relativeToPath:#"/Documents" usingResourceFork:NO];
I have a "test" subdirectory and I want to zip it up into test.zip. The "result" returns 1, but no zip folder appears. Any ideas?
Figured it out. For anyone with the same problem, you just need to set the relative path to the actual documents folder:
NSArray *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentFolder = [documentPath objectAtIndex:0];
ZKFileArchive *archive = [ZKFileArchive archiveWithArchivePath:#"Documents/test.zip"];
NSInteger result = [archive deflateDirectory:#"/Documents/test" relativeToPath:documentFolder usingResourceFork:NO];

How to hide folders created in Document Directory in ios?

I have created some PDF files programatically, which i am storing into the devices memory using the following code >>>>
NSString *fileName = [NSString stringWithFormat:#"SampleTextFile.pdf",strFinalString];
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *saveDirectory = [path objectAtIndex:0];
NSString *saveFileName = fileName;
NSString *documentPath = [saveDirectory stringByAppendingPathComponent:saveFileName];
I can see the file in the Devices Document folder.
I want to hide these files so that the user can not see or delete it.
Can anyone help me out to do this.
A good place to store private data is in ~/Library/Application Support/, which is the folder used on the Mac for this purpose.
You can generate a path to this folder using:
NSString *appSupportDir = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) firstObject];
You'll have to create the folder yourself the first time you use it, which you can do with:
if (![[NSFileManager defaultManager] fileExistsAtPath:appSupportDir])
{
[[NSFileManager defaultManager] createDirectoryAtPath:appSupportDir withIntermediateDirectories:YES attributes:nil error:NULL];
}
I wrote a simple library that makes this and all other useful iOS folders available as methods on NSFileManager: https://github.com/nicklockwood/StandardPaths
Just prefix the filename with a dot, as in .SampleTextFile.pdf.
But the real solution is to not store the document in the NSDocumentDirectory in the first place. You should create subdirectory in the NSLibraryDirectory and store this stuff there. It also gets backed up and will not get purged like Caches and tmp, but the user cannot access it with iTunes.

FMDB open database

I'm trying to open a database that I have in my project inside Resources.
The problem is that it seems that its impossible to find the database file!
I tried with the complete path, and it works, but this is not a good solution.
I would like to now how to open it!
I'm using this code:
db = [FMDatabase databaseWithPath:#"bbdd.sql"];
I don't know how to find the other part of the "actual" path.
Do you have a solution for me?
Thanks!!!!
You need to find the full path of the database in the your resource bundle, something like this :
NSString *databasePath = [[NSBundle mainBundle] pathForResource:#"mySQLiteDatabaseFile" ofType:#"sqlite3"];
There's a complete example in this thread Copying data to the Application Data folder on the iPhone
Get Document directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *dbPath = [documentDirectory stringByAppendingPathComponent:#"Test.db"];
Next
DB = [FMDatabase databaseWithPath:dbPath];

Resources