简体   繁体   English

macOS / Cocoa:用户将其移动到磁盘后,如何获取正在运行的应用程序主包的路径?

[英]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应用中,如果您需要获取应用路径或捆绑中的任何资源,则当然可以使用NSBundlemainBundle静态方法:

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.

相关问题 在适用于macOS的Cocoa应用程序中,我使用NSFileWrapper将捆绑包保存到磁盘。 如何使取景器为我的捆绑包显示正确的图标? - In a Cocoa Application for macOS, I save a bundle to disk using NSFileWrapper. How can I get the finder show the correct icon for my bundle? 如何在 macOS 中获取用户特殊文件夹的路径? - How to get the path of user's special folder in macOS? 应用程序处于非活动状态后,主线程在Cocoa应用程序中停止正常运行 - Main thread stops running properly in Cocoa app after a bit when app is inactive 如何在可可应用程序中读取macOS桌面文件 - How to read macOS desktop file in cocoa app 如何在Macos的Cocoa应用程序中创建新工作区? - How to make a new workspace in Cocoa app for MacOs? Cocoa 获取SIMBL Plugin当前运行路径 - Cocoa get current running path of SIMBL Plugin 在cocoa app bundle中的plist文件中显示“获取”用户权限 - Showing ‘fetching’ for user permission in plist file inside the cocoa app bundle 在Cocoa macOS App中,如何获取MKMapView左下角的度坐标? - In a Cocoa macOS App, how can I get the degree coordinates of the bottom left corner of a MKMapView? 可可应用程序的大小为/ dev / disk0s1 - Cocoa application get size of /dev/disk0s1 单击时重新绘制可可应用程序的主窗口 - Redrawing the cocoa app's main window when clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM