开发者

iOS: Copying file from bundle on application start-up

When the user first uses my application, it should copy a configuration file from the bundle into some folder. The user can then fiddle with this file, and if they mess it up they can simply press ' restore ' which will delete the file and copy it again from the bundle.

- (void) resetPresets
{
    LOG_( @"Copying tunings file from original..." );

    // copy default tunings -> curr tunings file

    NSString* appSupportDir = [NSFileManager appSupportDir];
    NSString* tuningsPath = [appSupportDir stringByAppendingPathComponent: @"tunings.txt"];

    NSBundle* bundle = [NSBundle mainBundle];
    NSString* origTuningsPath = [bundle pathForResource: @"tuningsOriginal"
                                                 ofType: @"txt" ];


    N开发者_StackOverflow中文版SFileManager* fileManager = [NSFileManager defaultManager];
    NSError* error = nil;

    if( [fileManager fileExistsAtPath: tuningsPath] )
    {
        [fileManager removeItemAtPath: tuningsPath
                                error: & error ];
        if( error ) 
            LOG( @"\n ERROR: %@ \n %@ \n", [error userInfo], [error localizedFailureReason] );
    }


    assert( [fileManager fileExistsAtPath: origTuningsPath] );

    [fileManager copyItemAtPath: origTuningsPath
                         toPath: tuningsPath
                          error: & error ];

    if( error ) 
        LOG( @"\n ERROR: %@ \n %@ \n", [error userInfo], [error localizedFailureReason] );


    LOG( @"done!" );


    // load profiles from it
    [self loadProfilesFromFile: tuningsPath ];

    // auto-sets active preset index to 0 & saves prefs
    self.activeThemeIndex = 0;
}

relies on a simple category:

#import "NSFileManager+addons.h"


@implementation NSFileManager ( NSFileManager_addons )

+ (NSString *) appSupportDir
{

    NSArray* paths = NSSearchPathForDirectoriesInDomains(
                                                         NSApplicationSupportDirectory,
                                                         NSUserDomainMask, 
                                                         YES
                                                         );

    NSString* appSupportDir = [paths objectAtIndex: 0];

    return appSupportDir;
}

@end

this is the line causing the problem:

    [fileManager copyItemAtPath: origTuningsPath
                         toPath: tuningsPath
                          error: & error ];

and this is the console output:

[presets init] Presets -> first run! Setting up with default presets Copying tunings file from original... ERROR: { NSDestinationFilePath = "/var/mobile/Applications/38FC3C65-74AF-4892-B48D-A3508A8CF404/Library/Application Support/tunings.txt"; NSFilePath = "/var/mobile/Applications/38FC3C65-74AF-4892-B48D-A3508A8CF404/Fork.app/tuningsOriginal.txt"; NSUserStringVariant = Copy; } No such file or directory

Why is it complaining that there is no such file or directory? Obviously there should not be such a file existing. when you copy a file to a new location you don't expect a file to be there.

so I guess it is complaining about the directory. But I have fished the directory out using a fairly standard method. what is going on? Is this not the right directory to be using? Or am I doing something else wrong?


The directories returned by NSSearchPathForDirectoriesInDomains are not guaranteed to exist; you need to create them yourself, if necessary (see the documentation). NSFileManager's createDirectoryAtPath:withIntermediateDirectories:attributes:error: can help with that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜