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

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.

Related

Data Saved in document Directory Dissappearing

My app stored downloaded audios and videos in document directory.Some users of app facing the problem that the content downloaded on their device getting disappeared with out any specific reason i.e app update. To save a file my path is as follows.
NSArray *dirPaths;
NSString *docsDir;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
docsDir = dirPaths[0];
NSString *filepath = [NSString stringWithFormat:#"%#/%#_%#_%#", docsDir,username,audioId,fileName];
[[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
The username , audioID and fileName in my app never change. So only option left is that somehow the path of my document directory is changing at the time when app try to search for the saved files, as described in Apple Doc. So i just want to know if i am thinking in the write direction or there is some other reason behind disappearing of content?
create your docs dir as below:
NSString * docsDir = [[dirPaths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:#"%#_%#_%#", username, audioId, fileName]];

XCode - file not found in document directory

I am having random error while reading saved photos from document directory in iPhone. I save photos taken from my app to document directory and then read it from there next time when user come back. However, after XCode 6 & base SDK change to 8.1, this document directory path keeps changing. So sometime I found photos and sometime not.
I read few posts online thats says that not Apple differentiate App from Data and that's why this issue coming up. Anyone has any thoughts on this? Any solution?
This is how I save file to document Directory
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [path firstObject];
NSString *filePath = [documentDirectory stringByAppendingPathComponent:#"key"];
[image writeToFile:path atomically:YES];
And this is how I read it:
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [path firstObject];
NSString *filePath = [documentDirectory stringByAppendingPathComponent:#"key"];
UIImage *cellImage = [[UIImage alloc] initWithContentsOfFile:filePath];
Go to Product - Scheme - Edit Scheme - Options - Working Directory and specify working directory

Downloading localize packages at runtime

I have an app on the market, I would like to localize it to other languages, the process I would like to implement is:
User downloads the app
User selects language
language pack is downloaded to applications folder
my question is how to implement step #3 (and does it effect the app review process) using adobe air
Thanks
Download your bundle to Documents directory with name "Translations.bundle", then use this code
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths firstObject];
NSString *bundlePath = [documentsPath stringByAppendingPathComponent:#"Translations.bundle"];
NSBundle *translationsBundle = [NSBundle bundleWithPath:bundlePath];
NSString *message = NSLocalizedStringFromTableInBundle(#"Message", nil, translationsBundle, nil);

SQLite data fetch from Document directory

I have a sqlite db stored in the Document directory and images name in one of the column of table ..and Image are store in app bundle.Images are coming directly by giving the names of imgaes in the column by using code..means no need to specify any path..just name is fine.
now if i store images dynamically in the document directory and give the name of the image in image column of Sqlite db directly as above I am unable to fetch it. Do i need to specify any path to reach there..as In app bundle images are coming directly.
Yes. You need to specify the path to the sqlite file in the document directory.
Or use the following code with corresponding image name (Images are in document directory):
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:#"img.png"];
yourImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfFile:imagePath]];
You can get directory path as below:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
and to get image you have to append image name after this path. You have data in your database, so from that you can get imagename.
If the value in the database is just the name of the image (such as "someimage.png") then you have two possibilities:
1) If the image is in the app bundle then use the value from the database with the UIImage imageNamed: method. I believe you already have this working.
2) If the image is in the Documents directory then you have a little more to do. You need to obtain the full path to the Documents directory and then append the filename (from the database) to the path. This will give you a full path to the image in the Documents directory. Then to load the image you need to use the UIImage imageWithContentsOfFile: method passing in the full file path.
Use this code .
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory35 = [paths objectAtIndex:0];
//Essential Info
NSString *fullPathEssentialInfo = [documentsDirectory35 stringByAppendingPathComponent:#"EssentialInfo.txt"];
This is implemented code.Thanks
There is an example project for accessing SQLite here: https://github.com/AaronBratcher/ABSQLite

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.

Resources