title: Storing and reading files from Documents directory iOS 5
body: <p>In my game, when a level is completed the app stores a "1" in a file in the Documents directory of the app.   When the game then loads, a player can only play a level if the previous level has been completed.  When I test the game via Xcode and on a device the app works properly and a level cannot be played until the previous level has been completed.  However, when the app was approved and released on the App Store, the app behaves as if each level has been completed (no locked levels).  I can't figure this one out and would appreciate someone's help!     The devices I'm testing on are all iOs 5.0 or higher.</p>
  
  <p>Below is the code that saves the completed level in the Documents directory:</p>
  
  <pre><code> NSMutableData* data = [[NSMutableData alloc] init];
          NSKeyedArchiver* coder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
          NSString *levelString = [NSString stringWithFormat:@"Level%d",level];
          [coder encodeInteger:1 forKey:levelString];
          [coder finishEncoding];
          NSString *levelString2 = [NSString stringWithFormat:@"Level%d.save",level];
          ///
          NSFileManager *filemgr;
          NSString *dataFile;
          NSString *docsDir;
          NSArray *dirPaths;
  
          filemgr = [NSFileManager defaultManager];
  
          // Identify the documents directory
          dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  
          docsDir = [dirPaths objectAtIndex:0];
  
          // Build the path to the data file
          dataFile = [docsDir stringByAppendingPathComponent:levelString2];
  
          // Check if the file already exists
          if ([filemgr fileExistsAtPath: dataFile])
          {
              [[NSFileManager defaultManager] removeItemAtPath:dataFile error:nil];
          }
          [data writeToFile:dataFile atomically:YES];
          [coder release];
          [data release];
      }
      @catch (NSException* ex) 
      {
          CCLOG(@"level save failed: %@", ex);
      }
  </code></pre>
  
  <p>Below is the code that reads the Document directory to see if the level has been completed:</p>
  
  <pre><code>if ([self loadCompletedLevels:6] == 1) { //// level gets unlocked **** }
  
  -(int) loadCompletedLevels:(int)theLevel; {
  
  int isLevelCompleted;  //1 = completed
  
  NSString* kSaveFile = [NSString stringWithFormat:@"Level%d.save",theLevel];
  NSString *levelString = [NSString stringWithFormat:@"Level%d",theLevel];
  
  @try
  {
  
      NSFileManager *filemgr;
      NSString *dataFile;
      NSString *docsDir;
      NSArray *dirPaths;
  
      filemgr = [NSFileManager defaultManager];
  
      // Identify the documents directory
      dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  
      docsDir = [dirPaths objectAtIndex:0];
  
      // Build the path to the data file
      dataFile = [docsDir stringByAppendingPathComponent:kSaveFile];
  
  
      if ([[NSFileManager defaultManager] fileExistsAtPath:dataFile])
      {
          NSData* data = [[NSData alloc] initWithContentsOfFile:dataFile];
  
          if (data &amp;&amp; [data length] &gt; 0)
          {
              NSKeyedUnarchiver* decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
              isLevelCompleted = [decoder decodeIntForKey:levelString];
  
              [decoder release];
          }
  
          [data release];
      }
      if (isLevelCompleted == 1) {
          levelCompleted = YES;
  
      }
  }
  @catch (NSException* ex) 
  {
  
      levelCompleted = NO;
  }
  return isLevelCompleted; }
  </code></pre>