Can't Find PLIST I Created in iOS App - ios

In my app, I am using Plists for making different lists. I want each Plist to be one list, and to be able to view all available lists in a TableView. To create the Plist I am using this:
NSString *plistname = [alertController.textFields[0].text stringByAppendingString:#".plist"];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *pathFirstTime = [documentsDirectory stringByAppendingPathComponent:plistname];
[#{} writeToFile: pathFirstTime atomically: YES];
I went in to finder after getting the path, and verified the Plist file IS there.
I then am creating an NSArray of all the Plist in documents directory to be able to load into a TableView using this code, but it returns an empty array:
NSBundle *bundle = [NSBundle mainBundle];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
self.files = [bundle pathsForResourcesOfType:#"plist" inDirectory:documentsDirectory];
NSLog(#"%#", self.files);

Related

pathForResource doesn't return an array

I have 2 methods, one for saving an array to a file and one for loading an array from the file, but when I'm trying to read an array, nothing happens, like an array is empty.
My code:
- (IBAction)write:(id)sender
{
NSArray *array = [racersArray copy];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryDirectory = [paths objectAtIndex:0];
NSString *location = [libraryDirectory stringByAppendingString:#"/history.plist"];
[array writeToFile:location atomically:YES];
}
- (IBAction)read:(id)sender
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"file" ofType:#"plist"];
NSArray *array = (path != nil ? [NSArray arrayWithContentsOfFile:#"/history.plist"] : nil);
}
You are reading/writing from/to two different places.
You write to the Library folder (it should be under Library/Application Support or some such, but I digress).
You read from the app bundle.
Try with this Methods may be helps you.👍
For Save
-(IBAction)save_Action:(id)sender{
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"manuallyData.plist"];
[plistData writeToFile:plistPath atomically:YES];
}
For Get Path
-(IBAction)retrive:(id)sender{
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"manuallyData.plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
plistPath = [[NSBundle mainBundle] pathForResource:#"manuallyData" ofType:#"plist"];
}
}
- (IBAction)savetapped:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"json.geojson"];
[paths writeToFile:plistPath atomically:YES];
}
- (IBAction)retriveTapped:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"json.geojson"];
NSLog(#"%#",documentsPath);
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
plistPath = [[NSBundle mainBundle] pathForResource:#"json" ofType:#"geojson"];
}
NSLog(#"%#",plistPath );
}
Took a while to figure out that I don't actually know what you are trying to do.
pathForResource gives you paths for resource in your application bundle. You can only read from there. You can never write there. Sometimes people have the initial version of a file in the application bundle and then write it somewhere else. If that's what you want then most likely you need to:
(1) write a method that returns the path where the array will be stored once your application has been running.
(2) The "write" method calls this path method to know where to write the array.
(3) The "read" method calls this path method to know where to read the array, then you read it. However, the array can be nil because your app was run the first time, or the array wasn't written correctly. In that case, you use pathForResource to get the path where the file would be in your application bundle, and read it from there.

App-only accessible folder in ios

I have an ipad app in which i show PDF documents in a UIWebView from my server.I want users to be able to download these documents so that they can read them offline. However i want them to read it only in the app.Basically my question is: Is it possible to have a folder that is only accessible from the app but not by users.
you can save it in the app bundle.
NSDate *date = [NSDate new];
NSString *docName = [NSString stringWithFormat:#"%lld.pdf", (long long)[date timeIntervalSince1970]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
dataPath = [documentsDirectory stringByAppendingPathComponent:docName];
EDIT: acording your comments question i think the solution would be something like this:
NSString *str = #"http://myserver.com/myfile.ext";
NSURL *url = [NSURL URLWithString:str];
NSData *content = [NSData dataWithContentsOfURL:url];
if (content) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"myfile.ext"];
[content writeToFile:dataPath atomically:YES];
}
Steps:
Create password to PDF file and Open PDF programatically with password.
You can Make Own DRM for PDF files.
Refer Following Link:
iPhone Documents Folder Library/Caches Security issue

How can I access my plist in another viewcontroller?

I have one 3 arrays that i am trying to save to 3 different plists in one view and then load that data into an array in another view. I am having trouble figuring out how to access the data in the plist in my other view and save it correctly in my initial view. Here is the code from my first view.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [documentPaths objectAtIndex:0];
NSString *documentsDirectory1 = [documentPaths objectAtIndex:1];
NSString *documentsDirectory2 = [documentPaths objectAtIndex:2];
NSString *documentsDirectory3 = [documentPaths objectAtIndex:3];
NSString *documentPlistPath1 = [documentsDirectory stringByAppendingPathComponent:#"balance.plist"];
NSString *documentsPlistPath2 = [documentsDirectory1 stringByAppendingPathComponent:#"principal.plist"];
NSString *documentsPlistPath3 = [documentsDirectory2 stringByAppendingPathComponent:#"interest.plist"];
NSString *documentsPlistpath4 = [documentsDirectory3 stringByAppendingPathComponent:#"dates.plist"];
[dateValues writeToFile:documentsPlistpath4 atomically:YES];
[balanceLabels writeToFile:documentPlistPath1 atomically:YES];
[pricipalLabels writeToFile:documentsPlistPath2 atomically:YES];
Any help is greatly appreciated!
Thanks!
create an array
NSArray *array = #[#"abc", #"cba"];
get path to plist file
NSString *documentsFolderPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *fileName = #"my_plist_file";
NSString *filePath = [documentsFolderPath stringByAppendingPathComponent:fileName];
save an array to .plist
[array writeToFile:filePath atomically:YES];
read an array from .plist
NSArray *arrayFromDisk = [NSArray arrayWithContentsOfFile:filePath];

Modifying and saving plist in iPhone app

I have an iPhone app where I have to modify the plist at runtime. I am successfully able to do it on simulator but when I run it on device it retrieves the data from the old plist and modification is not done. This is the code I am using for getting the path of the plist -
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"Customer.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path])
{
path = [[NSBundle mainBundle] pathForResource:#"Customer" ofType:#"plist"];
}
NSMutableDictionary *mainDictionary;
mainDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
To write a dictionary on plist file this line may work
[dicionaryToWrite writeToFile:path atomically: TRUE];

unable to save plist ios

I m able to save the plist file in Simulator but I m not able to save the Plist file in the device. Any suggestion.
I m using
NSString* dictPath = [[NSBundle mainBundle] pathForResource:#"Dictionary" ofType:#"plist"];
NSDictionary * dict = [[NSDictionary alloc] initWithContentsOfFile:dictPath];
to read the files
and
[dict writeToFile:dictPath atomically: YES];
to write to file.
You can not write in to main bundle. You only can read from main bundle. If you want to write an file you need to place it in to the documents directory of your app.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.plist",plistName]];
If you need the plist from the main bundle you can copy it first in to the documents directory then modify it. It is advised to have a check to ensure it is copied only once.
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.plist",plistName]];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path]){
NSLog(#"File don't exists at path %#", path);
NSString *plistPathBundle = [[NSBundle mainBundle] pathForResource:plistName ofType:#"plist"];
[fileManager copyItemAtPath:plistPathBundle toPath: path error:&error];
}else{
NSLog(#"File exists at path:%#", path);
}
Generally you would store these in ~/Documents or in ~/Library depending on the file. The question What is the documents directory (NSDocumentDirectory)? includes the documentation links and sample code you need to understand this.
You need to save the plist in Documents directory
to write
NSString *mainBundlePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *dictPath = [mainBundlePath stringByAppendingPathComponent:#"Dictionary"];
NSDictionary * dict = ...; //Construct your dictionary
[dict writeToFile:dictPath atomically: YES];
to read
NSDictionary * dict = [[NSDictionary alloc] initWithContentsOfFile:dictPath];

Resources