2011-07-21 17 views
1

當用戶第一次使用我的應用程序時,它應該將配置文件從軟件包複製到某個文件夾中。然後用戶可以擺弄這個文件,如果他們搞砸了,他們可以簡單地按'恢復',這將刪除該文件並再次從該文件夾中複製。iOS:在應用程序啓動時從軟件包複製文件

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


    NSFileManager* 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; 
} 

依賴於一個簡單分類:

#import "NSFileManager+addons.h" 


@implementation NSFileManager (NSFileManager_addons) 

+ (NSString *) appSupportDir 
{ 

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

    NSString* appSupportDir = [paths objectAtIndex: 0]; 

    return appSupportDir; 
} 

@end 

這是導致問題的行:

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

,這是控制檯輸出:

[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

爲什麼它抱怨沒有蘇ch文件或目錄?顯然,不應該有這樣的文件存在。當你將一個文件複製到一個新的位置時,你不希望有一個文件在那裏。

所以我想它是抱怨目錄。但我使用相當標準的方法將目錄撈出。到底是怎麼回事?這不是正確的目錄嗎?還是我在做其他事情?

回答

相關問題