0

I made a code that allows to write lines a txt file. On the Xcode simulator the writing works fine but I installed the application on my iPhone and the writing does not work. Below there is the code that allows to write the lines. I created several NSString that I then put together in a global NSString.

-(void)write_simpletxt{
 NSString *masse_totale_value_equipage = [[NSUserDefaults standardUserDefaults] stringForKey:@"masse_totale_value_equipage"];
 NSString *carburant_equipage = [[NSUserDefaults standardUserDefaults] stringForKey:@"carburant_equipage"];

 float carburant = [carburant_equipage floatValue];
 float masse = [masse_totale_value_equipage floatValue];

 NSURL *url = [self generateFileURL:self.documentName];

 NSString *title = @"/* PLANE CALCULATEUR */\n\n/* RAPPORT CALCULS SIMPLE*/\n\n";
 NSString *value = [NSString stringWithFormat:@"masse totale: %.0fKg. \nCarburant utile: %.0f\n", masse, carburant];


 NSString *complete = [NSString stringWithFormat:@"%@%@", title,value];
 [complete writeToURL:url atomically:YES encoding:NSStringEncodingConversionAllowLossy error:nil];

}

I think it's a problem of the number of bytes.

jscs
  • 63,694
  • 13
  • 151
  • 195
  • `generateFileURL:` What's that? Are you writing in Main Bundle ? Or in `NSHomeDirectory()`, etc.? – Larme May 15 '17 at 12:49
  • I put a .txt file in the main folder of my application. Then I generate an URL and I write in the file that corresponds to the url. –  May 15 '17 at 12:52
  • 2
    That's what I thought. You can't write into your Bundle App, but Simulator allows it. You need to "copy" your file at first use, and then read it from that other location. (You can check if you files exists at that new location, else, you read it from bundle, and then write it elsewhere). – Larme May 15 '17 at 12:53
  • I do not understand. Can you send me a code that explains your solution? thank you, –  May 15 '17 at 12:58
  • Read all the answers/comments from Larme's post, they explained everything. –  May 15 '17 at 13:48
  • Can anyone explain to me clearly how to create a text file and write NSString in ?? This is important for my project ! My code doesn't work –  May 15 '17 at 14:08

0 Answers0