Unable to create Plist of NSStrings [duplicate] - ios

This question already has answers here:
Write to string in plist dictionary
(2 answers)
Closed 8 years ago.
hi am trying to create a plist of my calculations but it turns out the list is empty....pls help here is my code.is there any othe way to write strings to a file other than this...
-(float)calculate
{
if(!self.operatorLabel.text.length)
return self.operand2;
float result=0;
switch([self.operatorLabel.text characterAtIndex:0])
{
case '+':result=self.self.operand1+self.operand2;
break;
case '-':result=self.operand1-self.operand2;
break;
case '*':result=self.operand1*self.operand2;
break;
case '/':if(self.operand2==0)
divideByZeroFlag=true;
else
result=self.operand1/self.operand2;
break;
default:self.operand1=0;
result=self.operand2;
break;
}
if(!divideByZeroFlag&&self.operand1!=0)
{
NSString *data=[NSString stringWithFormat:#"%g%#%g=%g",self.operand1,self.operatorLabel.text,self.operand2,result];
NSMutableDictionary *dat;
[dat setObject:data forKey:#"nthing"];
[dat writeToFile:#"memory.plist" atomically:YES];
}
return result;
}

1 - You must initialize dat.
2 - [NSDictionary writeToFile] expects full path, not just the name.
EDIT:
To create a path, do this:
+ (NSString*) createFullFilePath:(NSString *)fileName
{
//Look at documents for existing file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#", fileName]];
NSFileManager* fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:path])
{
NSError *error;
[fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
if (error)
NSLog (#"%#", [error localizedDescription]);
}
return path;
}
Once done, write to that path using following code (note that this initializes dictionary too (check NSLogs to see what you are doing yields results):
NSString * path = [self createFullFilePath:#"memory.plist"];
NSLog (#"#%", path);
NSMutableDictionary *dat = [NSMutableDictionary dictionary];
NSLog (#"#%", dat);
[dat setObject:data forKey:#"nthing"];
[dat writeToFile:path atomically:YES];

Why a plist file? A plist is nice for arrays and dictionaries, but I see no reason to store a simple string in a plist.
How about...
NSString *data = ...
NSError *error = nil;
[data writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];

Related

Storing Downloaded PDF To Documents Directory Not Working

I have the following code, I don't want to get into why I am doing it this way, but for some reason this is not working. The stringURL is working fine, it gets data back, but fails to write to the document directory. This is the first time I'm working with files, and have been pulling my hair out trying to get this to work. Please could someone point me in the right direction?
+ (void) downloadAndStorePDFFromURLWithString: (NSString *) stringURL andFileID: (NSString *) fileID andTitle: (NSString *) title;
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *pdfData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: stringURL]];
dispatch_async(dispatch_get_main_queue(), ^(void) {
//STORE THE DATA LOCALLY AS A PDF FILE
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
documentsDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat: #"%#/%#", fileID, title]];
//GET LOCAL FILE PATH OF DOWNLOADED PDF
//NSLog(#"SUCCESSFULLY DOWNLOADED DOCUMENT FOR FILE: %# WILL BE STORED AT %#", fileID, documentsDirectory);
BOOL success = [pdfData writeToFile: documentsDirectory atomically: YES];
NSLog(success ? #"Yes" : #"No");
//TELL TABLEVIEW TO RELOAD
//[[NSNotificationCenter defaultCenter] postNotificationName: #"DocumentDownloaded" object: nil];
//SAVE FILEPATH URL IN NSUSERDEFAULTS
//[PDFDownloadManager addURLToListOfSavedPDFs: [PDFDownloadManager filePath: fileID andTitle: title] andFileID: fileID];
});
});
}
You are attempting to write the file to a subfolder of the Documents folder. This is failing because the subfolder doesn't exist. You need to create the folder before you can write to it.
You should also clean up the code a bit. And use the better NSData method to write the file.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *folder = [documentsDirectory stringByAppendingPathComponent:fileID];
[[NSFileManager defaultManager] createDirectoryAtPath:folder withIntermediateDirectories:YES attributes:nil error:nil];
NSString *filePath = [folder stringByAppendingPathComponent:title];
NSError *error = nil;
BOOL success = [pdfData writeToFile:filePath options: NSDataWritingAtomic error:&error];
if (!success) {
NSLog(#"Error writing file to %#: %#", filePath, error);
}

Load a saved image from documents directory

I have used the code given here to save and load images
It works fine when i use it together in one view controller, but when i use the saveImage method in one view controller and try to load the image in another view controller the image returned blank...
In view controller A i use the following to save a image
- (void)saveImage: (UIImage*)image
{
NSLog(#"saveimage called");
if (image != nil)
{
NSLog(#"Image not null");
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
#"test.png" ];
NSData* data = UIImagePNGRepresentation(image);
[data writeToFile:path atomically:YES];
QrImageView.image = nil;
}
}
And in view controller say B I'm loading the image using..
- (UIImage*)loadImage
{
NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
#"test.png" ];
UIImage* image = [UIImage imageWithContentsOfFile:path];
// Write out the contents of home directory to console
NSLog(#"Documents directory: %#", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]);
return image;
}
I also get the content of the file in console but i dont understand why the image is blank
2013-07-06 14:13:19.750 YouBank[500:c07] Documents directory: (
"test.png"
)
Any idea what I'm doing wrong...
EDIT:
In the view controller B on the viewDidload method i do the following
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
ImageView.image=[self loadImage];
ImageName.text = #"Cash Withdrawal";
}
The problem is that you're updating your UIImageView with the UIImage only upon viewDidLoad. You need to add an update once you are done obtaining it from the filesystem (preferably on a background thread). So it would look something like this:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^{
UIImage *loadedImage = [self loadImage];
dispatch_async(dispatch_get_main_queue(),^{
ImageView.image = loadedImage;
});
});
I would also recommend to use names starting with lowercase characters for instance names, so you should rename ImageView -> imageView
try this
//Document Directory
#define kAppDirectoryPath NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)
#pragma mark - File Functions - Document/Cache Directory Functions
-(void)createDocumentDirectory:(NSString*)pStrDirectoryName
{
NSString *dataPath = [self getDocumentDirectoryPath:pStrDirectoryName];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:NULL];
}
-(NSString*)getDocumentDirectoryPath:(NSString*)pStrPathName
{
NSString *strPath = #"";
if(pStrPathName)
strPath = [[kAppDirectoryPath objectAtIndex:0] stringByAppendingPathComponent:pStrPathName];
return strPath;
}
When your write photo
[self createDocumentDirectory:#"MyPhotos"];
NSString *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:strImageName];
[UIImagePNGRepresentation(imgBg.image) writeToFile:pngPath atomically:YES];
When you get the file
Edit
NSError *error = nil;
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[FunctionManager getDocumentDirectoryPath:#"MyPhotos"] error:&error];
if (!error) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"self ENDSWITH '.png'"];
NSArray *imagesOnly = [dirContents filteredArrayUsingPredicate:predicate];
for (int i=0;i<[imagesOnly count]; i++) {
[arrSaveImage addObject:[imagesOnly objectAtIndex:i]]; //arrSaveImage is Array of image that fetch one by one image and stored in the array
}
}
NSString *strPath=[self getDocumentDirectoryPath:#"MyPhotos"]; // "MyPhotos" is Your Directory name
strPath=[NSString stringWithFormat:#"%#/%#",strPath,[arrSaveImage objectAtIndex:i]]; //You can set your image name instead of [arrSaveImage objectAtIndex:i]
imgBackScroll.image=[UIImage imageWithData:[NSData dataWithContentsOfFile:strPath]];
it may help you.

(iOS) Reading a plist from Documents folder - I get correct path but can't load dictionary

I know there are tons of threads already on this issue, but I cannot seem to find one that solves my problem. I have a plist with a dictionary as the root, containing three arrays. My code to write to the plist works fine in the simulator but is (null) on the device.
I'm not trying to write to the app bundle,
My file path is correct and I am checking at launch to make sure the file exists in the Documents folder (and it does exist).
- (void) writeToPlist:(NSString *)fileName playerColor:(NSString *)player withData:(NSArray *)data
{
NSArray *sysPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory ,NSUserDomainMask, YES);
NSString *documentsDirectory = [sysPaths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];
NSLog(#"File Path: %#", filePath);
NSDictionary *plistDict = [[NSDictionary alloc] initWithContentsOfFile:filePath];
NSLog(#"plist: %#", [plistDict description]);
[plistDict setValue:data forKey:player];
BOOL didWriteToFile = [plistDict writeToFile:filePath atomically:YES];
if (didWriteToFile) {
NSLog(#"Write to file a SUCCESS!");
} else {
NSLog(#"Write to file a FAILURE!");
}
}
Debug output:
File Path: /var/mobile/Applications/CA9D8884-2E92-48A5-AA73-5252873D2571/Documents/CurrentScores.plist
plist: (null)
Write to file a FAILURE!
I've used this same method in other projects, so I don't know if I'm forgetting something or what the deal is. I've checked spelling/capitalization and remade the plist, neither made any difference.
So why is plistDict (null) on the device, but not on the simulator? I apologize if I missed the solution in another post on plists.
Your code is written to assume the file already exists in the Documents folder. This won't be the case the first time this method is ever called.
You should add a check for the existence of the file. If it's there, load it. If not, do some other proper initialization of your data in preparation for writing.
Also, your dictionary needs to be mutable for you to change or add keys/values.
- (void) writeToPlist:(NSString *)fileName playerColor:(NSString *)player withData:(NSArray *)data
{
NSArray *sysPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory ,NSUserDomainMask, YES);
NSString *documentsDirectory = [sysPaths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];
NSLog(#"File Path: %#", filePath);
NSMutableDictionary *plistDict; // needs to be mutable
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
} else {
// Doesn't exist, start with an empty dictionary
plistDict = [[NSMutableDictionary alloc] init];
}
NSLog(#"plist: %#", [plistDict description]);
[plistDict setValue:data forKey:player];
BOOL didWriteToFile = [plistDict writeToFile:filePath atomically:YES];
if (didWriteToFile) {
NSLog(#"Write to file a SUCCESS!");
} else {
NSLog(#"Write to file a FAILURE!");
}
}

NSKeyedArchiver archiveRootObject and NSFileProtectionComplete/NSFileProtectionCompleteUnlessOpen

Is it actually possible to call NSKeyedArchiver archiveRootObject and work with data protection API setting the file attribute to NSFileProtectionComplete or NSFileProtectionCompleteUnlessOpen ?
First attempt to write the object to the specific location, in this case docFile (this is set to the documents directory). Then apply the file attributes to the docFile.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex: 0];
NSString* docFile = [docDir stringByAppendingPathComponent: #"Somefile.plist"];
NSError* error;
if([NSKeyedArchiver archiveRootObject:tempData toFile:docFile]) {
NSDictionary *fileAttributes = [NSDictionary
dictionaryWithObject:NSFileProtectionComplete
forKey:NSFileProtectionKey];
if([[NSFileManager defaultManager] setAttributes:fileAttributes
ofItemAtPath:docFile error: &error]) {
NSLog(#"Success");
}
else {
NSLog#(%#", error);
}
}

getting data from plist to NSMutableArray and writing the NSMutableArray to same plist iPhone

Using this code I am trying to get the data from Templates.plist file but I am getting null value in array.
//from plist
NSString *path = [[NSBundle mainBundle] pathForResource:#"Templates" ofType:#"plist"];
NSMutableArray *arry=[[NSMutableArray alloc]initWithContentsOfFile:path];
NSLog(#"arrayArray:::: %#", arry);
This is my plist file:
Also I want to know how can I add more strings to this plist file and delete a string from this plist.
First off you cannot write to anything in you mainBundle. For you to write to you plist you need to copy it to the documents directory. This is done like so:
- (void)createEditableCopyOfIfNeeded
{
// First, test for existence.
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:#"Template.plist"];
success = [fileManager fileExistsAtPath:writablePath];
if (success)
return;
// The writable file does not exist, so copy from the bundle to the appropriate location.
NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"Template.plist"];
success = [fileManager copyItemAtPath:defaultPath toPath:writablePath error:&error];
if (!success)
NSAssert1(0, #"Failed to create writable file with message '%#'.", [error localizedDescription]);
}
So calling this function will check if the file exists in the documents directory. If it doesn't it copies the file to the documents directory. If the file exists it just returns. Next you just need to access the file to be able to read and write to it. To access you just need the path to the documents directory and to add your file name as a path component.
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [docDir stringByAppendingPathComponent:#"Template.plist"];
To get the data from the plist:
NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
To write the file back to the documents directory.
[array writeToFile:filePath atomically: YES];
-(NSMutableArray *)start1
{
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:#"Templates.plist"];
if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]){
plistArr = [NSMutableArray arrayWithContentsOfFile: plistPath];
}
else {
plistArr = [[NSMutableArray alloc] init];
}
return plistArr;
}
- (void)createNewRecordWithName:(NSMutableDictionary *)dict
{
plistArr=[self start1];
[plistArr addObject: dict];
//[dict release];
[self writeProductsToFile:plistArr];
}
- (void)writeProductsToFile:(NSMutableArray *)array1 {
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:#"Template.plist"];
[array1 writeToFile:plistPath atomically:YES];
}
To get a plist into a mutable array, use this class method:
NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:path];
Then, add/delete strings from the array. To save the array back to a plist, use:
[array writeToFile:path atomically:YES];

Resources