Pergunta

In my iOS application, I allow user's to save their UI presets.

NSArray* theDirs = [[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory
                                                          inDomains:NSUserDomainMask];
NSURL *supportDir = [theDirs objectAtIndex:0];
NSURL *presetURL = [supportDir URLByAppendingPathComponent:@"Presets"];

// Write to disk.
NSData *presetData = [NSPropertyListSerialization dataWithPropertyList:mainDict format:NSPropertyListBinaryFormat_v1_0 options:0 error:nil];
NSString *presetPath = [[presetURL path] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.preset", presetName]];
if ([[NSFileManager defaultManager] fileExistsAtPath:presetPath]) {
    [[NSFileManager defaultManager] removeItemAtPath:presetPath error:nil];
}
[[NSFileManager defaultManager] createFileAtPath:presetPath contents:presetData attributes:nil];

I have just released an update on the app store for iOS7. User's have emailed me complaining that their presets have been erased upon updating....

Is it the directory I've chosen to use (support) ???

Any help would be appreciated!!

Foi útil?

Solução

Try using the document's directory. You can access it like this:

NSArray *directoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *directory = [directoryPath objectAtIndex:0];

You should be free to add/remove files/directories to your hearts content here.

Outras dicas

Probably the issue is with this code:

NSArray* theDirs = [[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask];

According to the Foundation Constants Reference NSApplicationSupportDirectory is:

NSApplicationSupportDirectory

Location of application support files (Library/Application Support).
Available in iOS 2.0 and later.
Declared in NSPathUtilities.h.

AFAIK there is no such folder exist in the iPhone device, probably that'll be the reason.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top