title: Saving JSON file to iPhone
body: <p>I want save json file from server each time, when user have internet connection to use it when iPhone doesnt have internet connection. But it doesn't working. Here is my code:</p>
  
  <pre><code>- (void)writeJsonToFile
  {
      //applications Documents dirctory path
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *documentsDirectory = [paths objectAtIndex:0];
  
      //live json data url
      NSString *stringURL = @"http://volodko.info/ic/json.php";
      NSURL *url = [NSURL URLWithString:stringURL];
      NSData *urlData = [NSData dataWithContentsOfURL:url];
  
      //attempt to download live data
      if (urlData)
      {
          NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"data.json"];
          [urlData writeToFile:filePath atomically:YES];
      }
      //copy data from initial package into the applications Documents folder
      else
      {
          //file to write to
          NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"data.json"];
  
          //file to copy from
          NSString *json = [ [NSBundle mainBundle] pathForResource:@"data" ofType:@"json" inDirectory:@"html/data" ];
          NSData *jsonData = [NSData dataWithContentsOfFile:json options:kNilOptions error:nil];
  
          //write file to device
          [jsonData writeToFile:filePath atomically:YES];
      }
  }
  </code></pre>