Loading JSON Data first then simulating-iOS - ios

Imagine that you are reading json data from the server continuously. let's say, you are getting weather data. I want to show only data 2 hours back from now. Whenever user clicks on the play button, it would show weather data on the map.
How I could save json data first then show it on the map. Because that would enhance my application instead of connecting server , getting data and showing it at the same time. please just give me advice. Do I need to save it first as a plist or an array? Where should I keep this data before I simulate?

Storing it in the plist in the documents directory is a good way of saving data for later reference.If you want to save the JSON string you can also opt for NSUSerDefaults..(preferably if the string is not too large)
Saving to plist..
NSURL *url = [NSURL URLWithString:serverPath];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/myplist.plist", documentsDirectory];
[urlData writeToFile:filePath atomically:YES];
}
and retrieving it..
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat: #"%#/myplist.plist",documentsDirectory ] ];
NSData *retrievedData = [NSData dataWithContentsOfFile:filePath];

Related

How to store Json data into Sqlite in ios

I have created one project like news update and i have saved in UITableView using JSON api to retrieving the data. now i'm getting problem like if internet is not there means records will not show to the user. now i'm planning to create data base like .sqlite. friends please help in crating data base. i'm new to this
Why not just simply save the json to a text file:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
NSString *filePath = [docDir stringByAppendingPathComponent:filePath];
[jsonString writeToFile:filePath atomically:YES];
and then read it:
NSString *jsonString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
here is the link that will useful for beginners like you..Database with ios
cheers

How to write data to my App document after take a photo from Camera?

It's possible to save the shooting pictures into the photo library through apply for writeImageDataToSavedPhotosAlbum: after took pictures by camera.
But I don't want to save the picture into the photo library and wanna to save them into my owned APP catalog. Currently, I can get the NSdata and Metadata.
Is there anybody could advise me what I should do? Really appreciated!!
To save the NSdata to your own app documents folder, use the below code
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirPath = [paths objectAtIndex:0];
NSString *filePath = [documentDirPath stringByAppendingPathComponent:#"ImageName.png"];
[yourNSdata writeToFile:filePath atomically:YES];
You can read it after using the below code,
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *imagePath = [documentsPath stringByAppendingPathComponent:filePath];
NSData *myImageData = [NSData dataWithContentsOfFile:imagePath];
UIImage *savedImage = [UIImage imageWithData:myImageData];

How to save image to iCloud using CoreData with MagicalRecord

I'm working for updating one of my app to use iCloud. all works perfectly, except for images. till now I saved the file path of the image in that way:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString*imageName = [[NSString alloc] initWithFormat:#"%#", self.fieldNome.text];
NSString *pngFilePath = [NSString stringWithFormat:#"%#/%#.png",documentsDirectory, imageName];
UIImage *image = self.myImageView.image;
NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(image)];
[data1 writeToFile:pngFilePath atomically:NO];
I think that to synchronize images between devices I have to save images to the ubiquitary container, but I can't figure out how to get the path and ho to save there. somebody could help me?

How to save multiple xml files in cache file path in ios?

Im saving xml file in cache file path as follows:
// Determile cache file path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
filePathl = [NSString stringWithFormat:#"%#/%#", [paths objectAtIndex:0],#"list.xml"];
// Download and write to file
NSURL *url = [NSURL URLWithString:detail_product_listing_rss];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePathl atomically:YES];
But using this code i can retrive data of recently used xml file.Could anybody please tell me how to save multiple xml files for offline use?
You need to save the xml with different names. Else it'll overwrite old xml.
Keep a integer value for this purpose, if you want this after the app restart also keep it in NSUserDefaults.
int posValue = [[[NSUserDefaults standardUserDefaults] objectForKey:#"lastXml"] intValue];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
filePathl = [NSString stringWithFormat:#"%#/list_%d.xml", [paths objectAtIndex:0],posValue];
NSURL *url = [NSURL URLWithString:detail_product_listing_rss];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePathl atomically:YES];
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:posValue+1] forKey:#"lastXml"];

My images have been deleted after an app update

I'm trying to update my iPad app using TestFlight and the problem I have is that the images which I'm storing are being deleted when I update the app.
I'm using this code to downloading and later store the images:
NSData *responseData = [request responseData];
UIImage *imageURL = [[UIImage alloc] initWithData:responseData];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(imageURL)];
[data1 writeToFile:documentsDirectory atomically:YES];
[imageURL release];
You are saving your image directly over (not inside) Documents directory. It should not even save the images in the first place. Instead create a subpath under Documents directory and save your file there.
NSString* imagePath = [documentsDirectory stringByAppendingPathComponent:#"someImage.png"];
[data1 writeToFile:imagePath atomically:YES];
The link which at the end solves my trouble was:
Problem with NSSearchPathForDirectoriesInDomains And Persistent Data
Thanks anyway!!

Resources