[英]macOS / Cocoa: How to get the path of the running app's main bundle after the user moves it on disk?
In macOS apps, if you need to get the path of your app or any resources in its bundle, you can of course use the mainBundle
static method of NSBundle
: 在macOS应用中,如果您需要获取应用路径或捆绑中的任何资源,则当然可以使用NSBundle
的mainBundle
静态方法:
NSBundle *bundle = [NSBundle mainBundle];
NSString *appPath = [bundle bundlePath];
NSString *resourceFolderPath = [bundle resourcePath];
... etc ...
However, if the user moves your app's bundle to a different location on disk while the app is running , then this method will not work. 但是,如果在应用程序运行时用户将应用程序的捆绑包移动到磁盘上的其他位置,则此方法将不起作用。 All of the above functions will still return the bundle's old paths from before the user moved it. 以上所有功能仍将返回用户移动包之前的包的旧路径。
How can you get the current path of your bundle or the path of any resource inside of it regardless of whether the user has moved it on disk? 无论用户是否将其移动到磁盘上,如何获取捆绑包的当前路径或其中的任何资源的路径?
So far, I've tried using NSRunningApplication
to get its current location like so: 到目前为止,我已经尝试使用NSRunningApplication
来获取其当前位置,如下所示:
NSRunningApplication *thisApp = [NSRunningApplication runningApplicationWithProcessIdentifier:getpid()];
NSLog(@"bundle URL: %@", [thisApp bundleURL]);
...but that too returns the old path of the bundle! ...但这也返回了捆绑包的旧路径!
Moving an application that's already running typically isn't supported. 通常不支持移动已经运行的应用程序。 If that's a scenario you really need to support then you can do the following: 如果您确实需要支持这种情况,则可以执行以下操作:
When the app is first launched, get the app's current path as you would normally and then create an NSURL bookmark for that path. 首次启动应用程序时,请像通常一样获取应用程序的当前路径,然后为该路径创建一个NSURL书签。
Whenever you need to know the app's current path you can resolve the NSURL bookmark you created at launch. 每当您需要了解应用程序的当前路径时,都可以解析在启动时创建的NSURL书签。 The bookmark will resolve to app's current path, even if the app has been moved. 书签将解析为应用程序的当前路径,即使该应用程序已移动。
More info on NSURL bookmarks is available here: https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/AccessingFilesandDirectories/AccessingFilesandDirectories.html#//apple_ref/doc/uid/TP40010672-CH3-SW10 有关NSURL书签的更多信息,请参见: https : //developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/AccessingFilesandDirectories/AccessingFilesandDirectories.html#//apple_ref/doc/uid/TP40010672-CH3-SW10
While macOS doesn't care if you move documents around as they're open, it does care if you move applications around. 尽管macOS不在乎是否在打开文档时移动文档,但它确实在乎是否移动应用程序。 This is a long-recognized issue in macOS development. 这是macOS开发中一个公认的问题 。
Instead of trying to fight it, app developers tend to either not care, or ask users if they want to move the application to the Applications directory at launch (since this is the most common move destination for apps). 应用程序开发人员没有尝试与之抗争,而是要么不在乎,要么询问用户是否要在启动时将其移至“应用程序”目录(因为这是应用程序最常见的移动目标)。 LetsMove is a project that demonstrates how you would do that. LetsMove是一个演示如何执行此操作的项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.