gpt4 book ai didi

ios - 将资源 plist 文件复制到应用程序支持文件夹?

转载 作者:行者123 更新时间:2023-11-28 23:27:24 26 4
gpt4 key购买 nike

有什么方法可以将 plist 文件复制到 iOS 中的 Application Support 文件夹?

下面的方法返回 Application Support 文件夹路径,但我需要将资源 plist 文件复制到该文件夹​​中。而不是使用 CopyItemAtUrl 方法,然后从包中删除。

- (NSURL*)applicationDataDirectory {

NSFileManager* sharedFM = [NSFileManager defaultManager];

NSArray* possibleURLs = [sharedFM URLsForDirectory:NSApplicationSupportDirectory
inDomains:NSUserDomainMask];
NSURL* appSupportDir = nil;
NSURL* appDirectory = nil;

if ([possibleURLs count] >= 1) {
// Use the first directory (if multiple are returned)
appSupportDir = [possibleURLs objectAtIndex:0];
}

// If a valid app support directory exists, add the
// app's bundle ID to it to specify the final directory.

if (appSupportDir) {
NSString* appBundleID = [[NSBundle mainBundle] bundleIdentifier];
appDirectory = [appSupportDir URLByAppendingPathComponent:appBundleID];
}

return appDirectory;
}

最佳答案

框架返回 Application Support 文件夹的正确路径,但与 Documents 文件夹不同,它不是默认创建的。

使用能够隐式创建文件夹的 NSFileManager 的替代 API

- (NSURL*)applicationDataDirectory {

NSFileManager* sharedFM = [NSFileManager defaultManager];
NSURL* appSupportDir = [sharedFM URLForDirectory:NSApplicationSupportDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil];
// add the app's bundle ID to it to specify the final directory.

NSString* appBundleID = [[NSBundle mainBundle] bundleIdentifier];
return [appSupportDir URLByAppendingPathComponent:appBundleID];
}

请注意,您也有责任创建子文件夹。

- (NSURL*)applicationDataDirectory {

NSFileManager* sharedFM = [NSFileManager defaultManager];
NSURL* appSupportDir = [sharedFM URLForDirectory:NSApplicationSupportDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil];
// add the app's bundle ID to it to specify the final directory.

NSString* appBundleID = [[NSBundle mainBundle] bundleIdentifier];
NSURL* appDirectory = [appSupportDir URLByAppendingPathComponent:appBundleID];
if (![appDirectory checkResourceIsReachableAndReturnError:nil]) {
[sharedFM createDirectoryAtURL:appDirectory withIntermediateDirectories:NO attributes:nil error:nil];
}
return appDirectory;
}

关于ios - 将资源 plist 文件复制到应用程序支持文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58197175/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com