Cocoa:获取应用程序可能的插件目录列表的正确方法?
在 Cocoa 应用程序中,通常我们可以在多个位置之一安装插件包。例如,如果应用程序名为“MyApp”,您可以在以下位置安装插件:
- /Applications/MyApp.app/Contents/PlugIns
- ~/Library/Application Support/MyApp/PlugIns
- /Library/Application Support/MyApp/PlugIns
- /Network/Library/Application Support/MyApp/PlugIns
我正在构建一个 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/Application Support/PlugIns”(缺少“MyApp”)文件夹之类的目录。在我开始破解代码以注入我的应用程序的名称(可能随时更改)之前,我做错了吗?
有没有办法告诉 Cocoa“给我这个应用程序的‘PlugIns’目录的所有搜索路径”?
In a Cocoa app generally we can install a plugin bundle in one of a number of places. If for example the app is called "MyApp" you'd be able to install the plugin at:
- /Applications/MyApp.app/Contents/PlugIns
- ~/Library/Application Support/MyApp/PlugIns
- /Library/Application Support/MyApp/PlugIns
- /Network/Library/Application Support/MyApp/PlugIns
I'm building an NSArray
of paths to search in the correct order but I'm pretty sure I'm doing this wrong since it feels like I'm doing too much work for something Apple seem to provide a lot of functions for.
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]];
This results in the Array returned by NSSearchPathForDirectoriesInDomains
, with the builtInPlugInsPath
value appended to the end.
However, it actually searches directories like "~/Library/Application Support/PlugIns" (missing the "MyApp") folder. Before I start hacking the code to inject the name of my application (which is subject to change at any time), am I doing this wrong?
Is there a way to just tell Cocoa "give me all search paths for 'PlugIns'" directories for this application"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有。你做对了。
您可以通过向主包询问其信息字典并在其中查找
kCFBundleNameKey
来在运行时获取应用程序的名称。重命名应用程序时,请更改 Info.plist 中的捆绑包名称。(绝对不要使用应用程序的文件名,因为它更加脆弱。)
请注意,如果用户安装的插件因为您重命名应用程序而停止工作,他们可能会不喜欢它。
请注意,上面的代码不会捕获应用程序包内的 PlugIns 文件夹。为此,请向主包询问其内置插件路径或 URL。
Nope. You're doing it right.
You can get the name of your application at run time by asking the main bundle for its info dictionary and looking for
kCFBundleNameKey
therein. When you rename your application, change the bundle name in your Info.plist.(Definitely do not use your application's filename, as that's much more fragile.)
Be aware that users might not like it if the plug-ins they installed stop working because you renamed your application.
Note that your code above will not catch the PlugIns folder inside the application bundle. For that, ask your main bundle for its built-in plug-ins path or URL.