Read and Write the MetaData contents as a File in Documents Directory - ios

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* arrayText = [MetaDataArray componentsJoinedByString: #"\n"];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"EmployeeData.txt"];
[arrayText writeToFile:path atomically:YES];
MetaData Array first Object look like this :
{fileSize:"9385033" labels:{viewed,starred,restricted,trashed,hidden}
originalFilename:"Chamak Challo - Ra.One (2011) [MP3-320Kbps-CBR].mp3" mimeType:"audio/mpeg"
title:"Chamak Challo - Ra.One (2011) [MP3-320Kbps-CBR].mp3" parents:[1]
md5Checksum:"51d598c750102dd4bca09addf4d8212d" quotaBytesUsed:"9385033"
lastModifyingUserName:"shadow.hibrise" copyable?:1
headRevisionId?:"0B7v4X9XjauwJR3dZM1FQWlpHeEFOZGYyYzR2NUdtLzhmb2FNPQ" kind:"drive#file"
writersCanShare:1 appDataContents?:0 modifiedDate:"2014-03-04T11:13:32.649Z" shared?:0
id:"0B7v4X9XjauwJdTUyeW5zTVRNTU0" ownerNames:[1] userPermission:{etag,kind,id,type,role}
createdDate:"2014-03-04T11:13:32.649Z" fileExtension:"mp3"
iconLink?:"https://ssl.gstatic.com/docs/doclist/images/icon_10_audio_list.png"
modifiedByMeDate:"2014-03-04T11:13:32.634Z" downloadUrl:"https://doc-10-3s-
docs.googleusercontent.com/docs/securesc/0ijvatgpdej0qafpk7qtp7n2jgndtb3d/ifb42us5ai6ae9ah4kalfu4cnrtl6j3n/1398153600000/18240796891762319987/18240796891762319987/0B7v4X9XjauwJdTUyeW5zTVRNTU0?h=16653014193614665626&e=download&gd=true" lastModifyingUser?:{kind,displayName,permissionId,isAuthenticatedUser} editable:1 etag:""fgLq6vWOgR-hiHy-psxfsLtIDgQ/MTM5MzkzMTYxMjY0OQ"" webContentLink:"https://docs.google.com/uc?id=0B7v4X9XjauwJdTUyeW5zTVRNTU0&export=download" owners?:[1] alternateLink:"https://docs.google.com/file/d/0B7v4X9XjauwJdTUyeW5zTVRNTU0/edit?
usp=drivesdk"}
I want to store Array of Objects as a file and vice versa.

You can use Sqllite or core data instead of filesystem.
For core-data please refer this link
For filesystem this can be useful

Related

Can we set data from a plist to Keychain in iOS?

Can we set data from a plist to Keychain in iOS? If we can do what is the correct approach. Also please let me know if there is any regular tutorial on the same.
Why do you want to save the data in keychain? probably because it is safe. Then you can use NSKeyedArchiver to save the encoded the plist data.
to store the encoded data in plist:
[NSKeyedArchiver archiveRootObject:data toFile:dataPath];
For your questions to get the data from plist file:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths firstObject];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"filename.plist"];
ObjectClassName *fetchedObject = [NSKeyedUnarchiver unarchiveObjectWithFile:dataPath];
to store the data in keychain:
[UICKeyChainStore setString:userPassword forKey:keychainKeyUserPassword];

Saved photos don't appear on device after update

On my iPhone app, I'm saving pictures associated with an event via this code:
[pngData writeToFile:filePath atomically:YES]; //Write the file
self.thisTransaction.picPath = filePath;
Later I retrieve and display the photo with this code:
UIImage * image = [UIImage imageWithContentsOfFile:thisTransaction.picPath];
Works great on my iPad (I don't have an iPhone).
However, if I update the app by connecting the iPad to my MB Pro after an Xcode code modification not involving the above lines, then disconnect and run it independently, the picture at the expected picPath is not retrieved. All other data associated with thisTransaction in Core Data is intact and unchanged, but the expected picture doesn't appear on the device after the update.
Can someone please tell me where I'm going wrong?
Edit to clarify file path construction
pngData = UIImagePNGRepresentation(capturedImage.scaledImage);
NSLog(#"1 The size of pngData should be %lu",(unsigned long)pngData.length);
//Save the image someplace, and add the path to this transaction's picPath attribute
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
int timestamp = [[NSDate date] timeIntervalSince1970];
NSString *timeTag = [NSString stringWithFormat:#"%d",timestamp];
filePath = [documentsPath stringByAppendingPathComponent:timeTag]; //Add the file name
NSLog(#"1 The picture was saved at %#",filePath);
The console log shows this filePath:
/Users/YoursTruly/Library/Developer/CoreSimulator/Devices/65FB33E1-03A7-430D-894D-0C1893E03120/data/Containers/Data/Application/EB9B9523-003E-4613-8C34-4E91B3357F5A/Documents/1433624434
The issue you are having is that the location of an app's sandbox can change over time. Typically this happens when an app is updated. So the worst thing you can do is persist absolute file paths.
What you need to do is persist just the part of the path relative to the base path (the "Documents" folder in this case).
Then when you want to reload the file again, append the persisted relative path to the current value of the "Documents" folder.
So your code needs to be something like this:
Save the file:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
int timestamp = [[NSDate date] timeIntervalSince1970];
NSString *timeTag = [NSString stringWithFormat:#"%d",timestamp];
filePath = [documentsPath stringByAppendingPathComponent:timeTag]; //Add the file name
[pngData writeToFile:filePath atomically:YES]; //Write the file
self.thisTransaction.picPath = timeTag; // not filePath
Load the file:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString *filePath = [documentsPath stringByAppendingPathComponent:thisTransaction.picPath];
UIImage *image = [UIImage imageWithContentsOfFile:filePath];

Reading and Writing NSMutableArray to iPhone not working (Works on iOS Simulator)

In an app I am working on I want to have an NSMutableArray (called pathsArray) that I can read from a file in the app's directory, be able create an instance of that array that I can add objects to and/or remove objects from, and then I want to write it back to the file. I have a UILabel that shows the number of contents in this array. My problem: my code below works fine on Xcode's iOS Simulator but when I try to run the app on my actual iPhone the data isn't saved. I know there are a lot of questions on here related to this issue but i can't seem to see what I am doing wrong. Any help would be greatly appreciated.
- (void) loadArrayContents {
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* filePath = [documentsDirectory stringByAppendingString:#"theArray"];
//Objects contained in an array returned by 'initWithContentsOfFile' are immutable even if the array is mutable
NSArray* contentsArray = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
pathsArray = [[NSMutableArray alloc] initWithArray:contentsArray];
}
and...
- (void) saveArrayContents {
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* filePath = [documentsDirectory stringByAppendingString:#"theArray"];
[pathsArray writeToFile:filePath atomically:YES]);
}
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"theArray"];
should solve the issue. The problem with
NSString *filePath = [documentsDirectory stringByAppendingString:#"theArray"];
is that it does not add / in the file path.

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

How can I permanently save AVAudioRecorder files for later access?

I have my code all set up to actually record the files. What I need, though, is a way to see where they are all saved and list them in a UITableView. I'm not asking the the implementation for the table; I only need a way to see where they are all saved.
All your data is saved into your application's document folder. You can access it with:
NSString *documentsDirectory;
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
if ([paths count] > 0) {
documentsDirectory = [paths objectAtIndex:0];
}
Assign an array to get a list of all your recorded files.
Your url where you are going to save the recorded file:
NSURL *urlOfRecordedFile = [NSURL fileURLWithPath:[NSString stringWithFormat:#"5#/recordFile.caf",[[NSBundle mainBundle] resourcePaht]]];
And here is the actual path of you file
NSLog(#"%#",url);

Resources