Data Saved in document Directory Dissappearing - ios

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]];

Related

Retrieve all pdf files store in iPhone memory iOS (Objective-C)

I want to retrieve all the pdf documents from my iPhone, including all the pdf files that are stored in other apps like Adobe Acrobat.
What I have now is:
NSString *path = [NSSearchPathForDirectoriesInDomains(NSAllLibrariesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
for (NSString *fileName in directoryContent) {
if ([fileName hasSuffix:#"pdf"]) {
//add files to an array
}
}
Which only points to one directory.
Firstly you are only getting the first path to the first directory, so you're only searching that one. Secondly, apple suggests to use the NSFileManager to search. Thirdly, be aware that developers of other apps can save their documents in different places, that you can't access or are not returned by these functions (Just so you are aware of this).
If you want to get array of all pdf files in Document directory, then use below code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSArray *arrPdfs = [[NSBundle bundleWithPath:[paths objectAtIndex:0]] pathsForResourcesOfType:#"pdf" inDirectory:nil];
arrPdfs will contain all pdfs in Document directory.

Swift Simulator folder location

I have found half a dozen answers about where the iPhone Simulator folder is. This does not display my iPad Applications though.
Does anyone know where the iPad Simulator folder location is or a way to get the location?
Direct to the directory containing all simulators:
Right Click on the "Finder" icon in your dock
Click on Go to Folder
~/Library/Developer/CoreSimulator/Devices/
Swift
Use this print statement:
print(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last)
Objective-C
You can find the exact location by using NSLog Print statement
NSLog(#"%#",[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]);
Then follow the first set of instructions with your location.
Put this code in your viewDidLoad
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(#"path-%#",documentsDirectory);
Then open finder in your mac. Press Command+Shift+G and input the "documentsDirectory" that was printed in console using NSLog. That will show your path for the app document directory.
The folder location has been changed, instead of going to iPhone Simulator you have to go to Core Simulator.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", cachesDirectory, fileName];
if ([[NSFileManager defaultManager]fileExistsAtPath:filePath]) {
return filePath;
}
else {
return filePath;
}
This code will show how you can check the files stored in the cache and how you can find the folder location.
Print description for the cache directory to get the path.

path of the directory where ibooks's pdf stored ios

I need to get the already existing pdf files in the device and to upload the selected pdf to the server. I've added one pdf file to iBooks and then tried searching that file in directory using this code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
//NSError *error = nil;
NSArray *filesAtPath = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:nil];
NSLog(#"filesAtpath:%#",filesAtPath);
for (NSString *path in filesAtPath)
{
NSLog(#"add..");
if ([path rangeOfString:#".pdf"].length > 0)
{
//Do whatever you want with the pdf file
NSLog(#"pdf: %#",path);
}
}
but the log shows empty array, could you please tell me, is there any other method to get the pdf list in the device and select one to upload in the server.
No, Apple does not provide access to iBooks library in the same way that it provides some access to the Music library.

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.

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