简体   繁体   中英

Code works on simulator but not on device

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"]]]];
}
}

`

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM