0

I have a json data to show some infos.. but i should generate "a file" to save these infos to my iPhone. Ideas?

- (void)showInfos {
NSMutableDictionary *infoDictionary = [[NSMutableDictionary alloc]init];
[infoDictionary setObject:self.totalDuration.text forKey:@"totalFrames"];

NSMutableArray *nodesArray = [[NSMutableArray alloc]init];
[nodesArray addObject:@{@"x":@(_zeroButton.frame.origin.x),@"y":@(_zeroButton.frame.origin.y),@"frame":self.zeroBtnLbl.text}];
[nodesArray addObject:@{@"x":@(_firstButton.frame.origin.x),@"y":@(_firstButton.frame.origin.y),@"frame":self.firstBtnLbl.text}];
[nodesArray addObject:@{@"x":@(_secondButton.frame.origin.x),@"y":@(_secondButton.frame.origin.y),@"frame":self.secondBtnLbl.text}];
[nodesArray addObject:@{@"x":@(_thirdButton.frame.origin.x),@"y":@(_thirdButton.frame.origin.y),@"frame":self.thirdBtnLbl.text}];
[nodesArray addObject:@{@"x":@(_fourthButton.frame.origin.x),@"y":@(_fourthButton.frame.origin.y),@"frame":self.fourthBtnLbl.text}];
[infoDictionary setObject:nodesArray forKey:@"nodes"];

NSLog(@"dicArray:%@",infoDictionary);
NSData *data = [NSJSONSerialization dataWithJSONObject:infoDictionary options:kNilOptions error:nil];
NSArray *retDicArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSLog(@"retDicArray:%@",retDicArray);
}
dicle
  • 1,122
  • 1
  • 12
  • 40

2 Answers2

3

You can write that data to file using writeToFile:atomically: method of NSData.

NSData *data       = [NSJSONSerialization dataWithJSONObject:infoDictionary options:kNilOptions error:nil];
NSString *docDir   = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *filePath = [docDir stringByAppendingPathComponent:@"MyJSONFile.json"];
[data writeToFile:filePath atomically:YES];
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • thank you for your answer. but where can i see that file in my phone... Couldnt find it.? @midhunMP – dicle Nov 02 '15 at 09:08
  • @deniz If you are using the iPhone simulator, it will be in your application's directory. If you are using a debug/development build then download the container using organiser of your Xcode and check it in the folder. In production/app-store build there is no direct way to check that except reading back the content. However you can enable iTunes file sharing in your app (But you must have a valid reason for App Store approval) – Midhun MP Nov 02 '15 at 09:47
  • but if someone wants to read the results, how he will get it... thats the main problem.. For example a windows user.. @midhunMP – dicle Nov 02 '15 at 10:06
  • @deniz: Unlike android or other platforms you can't expose the app data directory of iOS (unless you root it). Either you should implement a custom directory explorer in your app to show the json files in your app and read back the data programmatically or send those files through a web-service. – Midhun MP Nov 02 '15 at 11:48
  • @deniz: If you write the file to the Documents directory and have [file sharing enabled](http://stackoverflow.com/questions/6029916/how-to-enable-file-sharing-for-my-app), you can access these files via iTunes (also on Windows). – DarkDust Nov 03 '15 at 16:21
0

Do you really need to save it into a file or you just want to serialize the information into the app?

Personally I often use the NSUserDefaults to save simple user information inside the app.

For example, if you want to store an integer:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:newDistance forKey:@"myInteger"];
[defaults synchronize];

Then when you want to retrieve it, simply do:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
int myInteger = (int)[defaults integerForKey:@"myInteger"];

You can store also NSArray, so it should work for you.

If you have more complicated structure to serialize you can use Core Data but it's a lot more complicated.

Thomas
  • 677
  • 6
  • 10
  • yes @pralthom .. i have to save infos into a file to compare infos with each other.. Do you have alternative solution? – dicle Nov 02 '15 at 09:09