How to find the residing directory of a OS X application package - programmatically

前端 未结 3 1294
予麋鹿
予麋鹿 2021-01-15 16:03

I have a OS X application as a .app package which can reside in any arbitrary location in the filesystem. Is there a way to find the current path of the package programmatic

3条回答
  •  不要未来只要你来
    2021-01-15 16:58

    You should be able to find the location of the .app by [[NSBundle mainBundle] bundleURL]; or [[NSBundle mainBundle] bundlePath];.

    However, you probably want to access resources inside the .app by going through NSBundle to locate them in most cases (this deals with issues like localization).

    Note: If you are planning to modify the application bundle; don't. You should not, can not and must not assume that you have write access to the application bundle; and if you do, something is likely very wrong.

    Edit: As a point of interest, if you are writing a C program on OS X and need to find the location of the executable; you can use a non-portable main declaration like so:

    int main (int argc, const char *argv[], const char *env[], const char *path[]) {
      // path[0] now contains the path to the executable
      // NOT PORTABLE! OS X ONLY!
      return 0;
    }
    

提交回复
热议问题