Question

I´m writing certain values to a file. See Write Operations below.
This works fine when using iPad 6.1 Simulator.

When trying the same thing on my iPad it fails. I think it´s something with sandboxing. I haven´t found out yet which path is best on iOS Devices to write stuff for internal use.

Any ideas?

#pragma mark Write Operations to Tmp Folder
    - (BOOL) psWriteFileWithName: (NSString*) fileName
                      withString:(NSString*) string {

        NSString *fileName = @"artistNumber";
        NSError * error = NULL;
        NSString *filePath = [NSString stringWithFormat:@"/tmp/%@.txt",fileName];
        [string writeToFile:filePath 
                 atomically:YES 
                   encoding: NSUTF8StringEncoding 
                      error:&error];
        return  YES;
    }
Was it helpful?

Solution

You cannot write to /tmp since this is outside of your app sandbox. However your app also has a temp directory, which can be referenced with the NSTemporaryDirectory() function:

Which works like:

NSString *tempfilePath =  [NSTemporaryDirectory() stringByAppendingPathComponent:filename];

Here is you method with the correct NSTemporaryDirectory() implementation, also edit some error handling:

#pragma mark Write Operations to Tmp Folder
- (BOOL) psWriteFileWithName: (NSString*) fileName
                  withString:(NSString*) string {

    NSString *fileName = @"artistNumber";
    NSError *error = nil;
    NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:filename];
    if (![string writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error] ) {
       NSLog(@"Error writing file: %@", error);
       return NO;
    }
    return  YES;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top