As my above screen shot that i have a json file (productDetail.json) in my app then i write file from json data like below code
else if([(MultipleURLConnection *)connection tag]==2)
{
if([fn writeToFile:responseData fileName:@"productDetail.json"])
[self performSelector:@selector(loadingJSON) withObject:nil afterDelay:0.1];
}
in above code responseData is NSMutableData and fn is function Class object name where i have below method
- (BOOL)writeToFile:(NSData *)data fileName:(NSString *)fileName
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *myFile = [documentsDirectory stringByAppendingPathComponent:fileName];
NSFileManager *fm = [NSFileManager defaultManager];
if (![fm fileExistsAtPath:myFile]) // if file doesn't exist, create it
return ([fm createFileAtPath:myFile contents:data attributes:nil]);
else
return ([data writeToFile:myFile atomically:YES]);
}
Now i decide to remove json file from app and to put it on server and then fetch it .so after putting json file on server i tried to fetch it like below coding
else if([(MultipleURLConnection *)connection tag]==2)
{
NSURL *url = [NSURL URLWithString:@"http://mysite.com/Infomist/productDetail.json"];
NSData *jsonData = [NSData dataWithContentsOfURL:url];
if(jsonData != nil)
{
NSError *error = nil;
id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
if (error == nil)
NSLog(@"show data %@",result);
}
Now above method successfully fetching my data from server it show all my data in NSlog but problem for me is now how to modify this Method now
if([fn writeToFile:responseData fileName:@"])
Beacuse after putting the json file on server i don't have any file to pass file name in above method to call the
- (BOOL)writeToFile:(NSData *)data fileName:(NSString *)fileName .
I already have App flow for local json file so i need only to write json data in my app.
- (BOOL)writeToFile:(NSData *)data fileName:(NSString *)fileName
the above requires NSData
as a parameter
id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
the above gives id
as a return type object..you need to check which type of object it is using isKindOfClass
and then later convert it to data using decoding/NSkeyedArchiver or other techniques