当用户第一次使用我的应用程序时,它应该将配置文件从软件包复制到某个文件夹中。然后用户可以摆弄这个文件,如果他们搞砸了,他们可以简单地按'恢复',这将删除该文件并再次从该文件夹中复制。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文件或目录?显然,不应该有这样的文件存在。当你将一个文件复制到一个新的位置时,你不希望有一个文件在那里。
所以我想它是抱怨目录。但我使用相当标准的方法将目录捞出。到底是怎么回事?这不是正确的目录吗?还是我在做其他事情?