-1

I made an app which writes text using "writeToURL" on a txt file attached to the resources. When I load the app on the simulator it writes the text you entered, but when you launch the app on the device it writes nothing. If you have something written on the txt file, the app loaded on the device shows it, but it can´t overwrite it, as the simulator does... what can be wrong? Thx! The code (iOS Objective C):

-(IBAction)cargarALaWeb:(id)sender{
if ([_texto.text isEqualToString:[NSString stringWithFormat:@""]]) {
    [_web1 loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://contactonotpad.wix.com/inicio#!ayuda-con-cargar-en-la-web/ak5ig"]]];

}
else{
NSString *textofile=_texto.text;

[textofile writeToURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"web" ofType:@"html"]] atomically:YES encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"Escrito");
    [_web1 loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"web" ofType:@"html"]]]];
}
}

`

JVE
  • 833
  • 6
  • 16
  • 2
    tag the question for the actual language you're using, and show the code you're using. include details of the URL you're trying to write to – Wain Apr 19 '16 at 11:39

2 Answers2

1

Any file you want to edit in the app bundle need to be copied into the app documents directory first, then you should edit and use the document from there. You shouldn't / can't edit the app bundle contents.

Wain
  • 118,658
  • 15
  • 128
  • 151
0

How to copy a file to the documents directory

Apples docs on the file system

The short answer. Apps cannot write to files in the bundle for security reasons. Each app is given a "Sandbox" in which files can be edited. You have to write code to copy the file from the bundle to the documents directory where the file can be edited.

Community
  • 1
  • 1
Jaybit
  • 1,864
  • 1
  • 13
  • 20