通过标识符查找包

通过标识符查找包

问题描述:

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

I want to create a bundle from an arbitrary bundle identifier
e.g. com.apple.iokit.IOStorageFamily

这并不是一件不合理的事情,因为应该假定捆绑包ID
唯一,但是明显的代码不起作用:

It's not an unreasonable thing to do as bundle IDs are supposed
to be unique, however the obvious code does not work:

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

此代码仅适用于已加载的包
(您好,鸡肉和鸡蛋问题),实际上,您有
了解有关标识符的更多信息
在您可以做任何事情之前.对于上面的ID
我grep出了最终的组件并将其转化为
/System/Library/Extensions/IOStorageFamily.kext
然后我按路径加载.

This code only works for bundles you've already loaded
(hello, chicken and egg problem), and in fact, you have
to know a little more than you'd like about the the identifier
before you can do anything. For the above style of ID
I grep out the final component and tranform it into
/System/Library/Extensions/IOStorageFamily.kext
which I then load by path.

这是最新技术吗?还是有更通用的方法?

Is this the state of the art or is there a more general way?

最近

KextManagerCreateURLForBundleIdentifier()<IOKit/kext/KextManager.h>中可能是 的使用,尽管我相信它只行得通 对于1)加载的kext, 或2)在/S/L/E/中.这是雪 豹头文件:

KextManagerCreateURLForBundleIdentifier() in <IOKit/kext/KextManager.h> may be of use, though I believe it only works for kexts that are either 1) loaded, or 2) in /S/L/E/. Here is the Snow Leopard headerdoc:

/*!
 * @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);

请注意,在雪豹"之前 可能仅适用于/S/L/E中的kexts;这 API存在,但没有 headerdoc描述其行为.

Note that prior to Snow Leopard, it may only work for kexts in /S/L/E; the API existed, but there was no headerdoc describing its behavior.

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

For me this worked really well on Mac OS X 10.5.