Does an iOS app have write access inside its bundle?

后端 未结 2 417
逝去的感伤
逝去的感伤 2020-11-27 07:34

I save some run-time generated files inside the .app bundle of my iOS app. In the simulator it works fine, in the device it crashes:

Could create outp

2条回答
  •  广开言路
    2020-11-27 08:05

    No, every time you change your bundle you invalidate your signature.

    If you want to write files you`l need to write in the best folder depending on what you want to do with that file.

    1. Documents folder for long duration files
    2. Cache for small operations
    3. and so on

    EDIT

    To get the path you`ll need something like this:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"filename.ext"];
    

    With this path you can write or read like this:

    write:

    NSString *content = @"One\nTwo\nThree\nFour\nFive";
    [content writeToFile:fileName atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];
    

    read:

    NSString *content = [[NSString alloc] initWithContentsOfFile:fileName usedEncoding:nil error:nil];
    

提交回复
热议问题