代码之家  ›  专栏  ›  技术社区  ›  Gordon Childs

按标识符定位捆绑包

  •  6
  • Gordon Childs  · 技术社区  · 16 年前

    我想从任意包标识符创建包
    例如 com.apple.iokit.IOStorageFamily

    这不是一件不合理的事情,因为包ID是应该的。
    但要保持唯一性,明显的代码不起作用:

    NSString* bID = @"com.apple.iokit.IOStorageFamily";
    NSBundle* bundle = [NSBundle bundleWithIdentifier:bID];
    

    此代码仅适用于已加载的包
    (你好,鸡和蛋的问题),事实上,你有
    比你想知道的更多
    在你做任何事之前。对于上述ID样式
    我将最后一个组件变灰并转换为
    /System/Library/Extensions/IOStorageFamily.kext
    然后我按路径加载。

    这是最先进的还是更一般的方法?

    6 回复  |  直到 13 年前
        1
  •  3
  •   Georg Schölly Crazy Developer    15 年前

    就在最近 Andrew Myrick answered a similar question 在Darwin Dev邮件列表上:

    KextManagerCreateURLForBundleIdentifier() 在里面 <IOKit/kext/KextManager.h> 可能是 有用,尽管我相信它只起作用 对于1)加载的kexts, 或2)in/s/l/e/。这是雪 豹头兽:

    /*!
     * @function KextManagerCreateURLForBundleIdentifier
     * @abstract Create a URL locating a kext with a given bundle identifier.
     *
     * @param    allocator
     *           The allocator to use to allocate memory for the new object.
     *           Pass <code>NULL</code> or <code>kCFAllocatorDefault</code>
     *           to use the current default allocator.
     * @param    kextIdentifier
     *           The bundle identifier to look up.
     *
     * @result
     * A CFURLRef locating a kext with the requested bundle identifier.
     * Returns <code>NULL</code> if the kext cannot be found, or on error.
     *
     * @discussion
     * Kexts are looked up first by whether they are loaded, second by version.
     * Specifically, if <code>kextIdentifier</code> identifies a kext
     * that is currently loaded,
     * the returned URL will locate that kext if it's still present on disk.
     * If the requested kext is not loaded,
     * or if its bundle is not at the location it was originally loaded from,
     * the returned URL will locate the latest version of the desired kext,
     * if one can be found within the system extensions folder.
     * If no version of the kext can be found, <code>NULL</code> is returned.
     */
    CFURLRef KextManagerCreateURLForBundleIdentifier(
        CFAllocatorRef allocator,
        CFStringRef    kextIdentifier);
    

    注意在雪豹之前 仅适用于Kexts in/s/l/e;The API存在,但没有 描述其行为的校长。

    对我来说,这在Mac OS X 10.5上非常有效。

        2
  •  9
  •   sabiland    16 年前

    用这个

    NSString *path = [[NSWorkspace sharedWorkspace] absolutePathForAppBundleWithIdentifier:@"com.apple.TextEdit"];
    
        3
  •  4
  •   Nik Gervae    16 年前

    我不认为MacOSX在任何地方都保存着所有包ID的全局数据库。

    如前所述,您可以使用nsworkspace以非常简单的方式找到应用程序。

    此外,由于您在示例中使用了kext,所以在Leopard(10.5)上有一个名为“kextfind”的工具,您可以运行该工具在System Exensions文件夹中搜索kext s(除非将该工具指向其他位置,否则不会找到其他位置的kext s)。kext find有很多选项——有关详细信息,请参见手册页——但是要按bundle id查找kext,可以这样做:

    kextfind -bundle-id com.apple.iokit.IOStorageFamily
    

    我们目前没有C级API来按包ID查找kexts。

    至于从bundle id的最后一个组件获取路径:不要这样做。不需要包装器名称来匹配bundle id的最后一个组件,我已经看到kexts(更不用说其他bundle了),其中两个不匹配。

        4
  •  0
  •   user23743    16 年前

    如果你要找的绝对是一个kext,那么你可以查看/s/l/es/文件夹中每个包的信息字典,直到找到你的包。除了应用程序(LaunchServices将在其中进行搜索)和已加载的bundle之外,没有按标识符搜索bundle。

        5
  •  0
  •   Louis Gerbarg    16 年前

    为了回答这个问题,我认为人们真的需要知道“为什么要用这种方式查看包标识符?”如果你总是可以在一些相当合理的地方搜索到kexts,如果它们是你可以使用ls的应用程序,我看不到你想同时使用这两种应用程序的情况,所以我看不到有一个共同的方法来做这件事的需要。

    应该注意的是,一个卷上可以有多个相同包标识符的实例。

        6
  •  0
  •   Pierre Lebeaupin    13 年前

    为了完整性起见,我应该提到您可以使用 kMDItemCFBundleIdentifier 聚光灯/元数据键;当然,您必须准备好处理多个(通常它们应该有不同的版本)。