可可:正确的方式来获得列可能的插件目录应用程序?
题
在一个可可应用程序,一般来说,我们可以安装一个插件捆绑在一些地方。例如,如果该程序被称为"MyApp"你可以安装的插件:
- /Applications/MyApp。应用程序/Contents/插件
- ~/Library/应用程序支助/MyApp/插件
- /Library/应用程序支助/MyApp/插件
- /网络/Library/应用程序支助/MyApp/插件
我要建一个 NSArray
路径搜索以正确的顺序但是我敢肯定,我这样做是错误的,因为它感觉像是我做的工作太多东西苹果看来提供了很多功能。
NSArray *systemSearchPaths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSAllDomainsMask, YES);
NSMutableArray *searchPaths = [NSMutableArray array];
NSFileManager *fileManager = [NSFileManager defaultManager];
for (NSString *systemPath in systemSearchPaths) {
NSString *systemPluginsPath = [systemPath stringByAppendingPathComponent:@"PlugIns"];
// FIXME: Remove debug code
NSLog(@"Considering plugin path %@", systemPluginsPath);
if ([fileManager fileExistsAtPath:systemPluginsPath]) {
[searchPaths addObject:systemPluginsPath];
}
}
[searchPaths addObject:[[NSBundle mainBundle] builtInPlugInsPath]];
这一结果,在返回的数组 NSSearchPathForDirectoriesInDomains
, , builtInPlugInsPath
值所附的结束。
然而,它实际上检索目录,如"~/Library/应用程序支助/插件"(缺失的"MyApp")的文件夹。之前我开始破解代码注的名字,我应用程序(这是受到改变,在任何时间),我这样做是错误的?
有没有办法只是告诉了可可"给我所有的搜索途径"插件""目录用于该应用程序"?
解决方案
不。你这样做是正确的。
你可以获得您的应用程序的名称在运行时间要求的主要束对其信息的字典中寻找 kCFBundleNameKey
在其中。当你重新命名你的应用,改变束名在您的信息。plist。
(绝对不使用应用程序 文件名, ,因为那样的更加脆弱。)
请注意,用户可能不喜欢它如果他们的插件安装停止工作,因为你改名为你的申请。
注意,你的代码不会赶上的插件夹内部应用程序包。为此,问问你的主束其内在的插件路径或网址。
不隶属于 StackOverflow