- 獲取Main Bundle
main bundle表示正在運行的app中所包含的code和resources。如果你是app開發(fā)者加缘,這個是最常用的bundle杭跪。main bundle也是最簡單獲取的bundle因為它不需要提供任何信息醋界。
mainBundle = [NSBundle mainBundle];
或者通過Core Foundation來獲取湖蜕,如果你在寫一個基于C的APP的話:
mainBundle = CFBundleGetMainBundle();
- 通過Path獲取Bundles
如果你想訪問一個非mainBundle的bundle,那么你可以創(chuàng)建一個合適的bundle對象叨吮,如果你知道這個bundel的路徑的話辆布。
myBundle = [NSBundle bundleWithPath:@"/Library/MyBundle.bundle"];
CFURLRef bundleURL;
CFBundleRef myBundle;
// Make a CFURLRef from the CFString representation of the
// bundle’s path.
bundleURL = CFURLCreateWithFileSystemPath(
kCFAllocatorDefault,
CFSTR("/Library/MyBundle.bundle"),
kCFURLPOSIXPathStyle,
true );
// Make a bundle instance using the URLRef.
myBundle = CFBundleCreate( kCFAllocatorDefault, bundleURL );
// You can release the URL now.
CFRelease( bundleURL );
// Use the bundle...
// Release the bundle when done.
CFRelease( myBundle );
- 通過Identifier找到Bundle:
通過bundle identifier來鎖定bundles是一個有效的方法,并且這種方法會讓bundle提前加載到內(nèi)存中茶鉴。
例如:
NSBundle* myBundle = [NSBundle bundleWithIdentifier:@"com.apple.myPlugin"];
如果要通過Core Foundation來使用identifier鎖定bundle要用以下方式:
CFBundleRef requestedBundle;
// Look for a bundle using its identifier
requestedBundle = CFBundleGetBundleWithIdentifier(
CFSTR("com.apple.Finder.MyGetInfoPlugIn") );
記住锋玲,你只可以通過bundle idenditfier來找已經(jīng)打開了的bundle。你不能使用這個方法來引用一個還沒有被load的插件涵叮。
- 想要在特殊的設(shè)備上加載文件資源惭蹂,需要在文件名上加上特殊的自定義字符串。格式如下:
<basename><device>.<filename_extension>
- basename代表了資源的原始名稱割粮;
- extension代表了文件的擴展名盾碗;
- device字符串可以為以下值:
~ipad,資源只能被iPad設(shè)備加載舀瓢;
~iphone廷雅,資源只能被iPhone或者iPod touch設(shè)備加載
加載資源時,你可以指定資源為MyImage京髓。png榜轿,讓系統(tǒng)選擇合適的版本,例如:
UIImage* anImage = [UIImage imageNamed:@"MyImage.png"];
在iPhone或者iPod touch上朵锣,系統(tǒng)會加載MyImageiphone.png資源;在iPad上甸私,MyImageipad.png資源文件會被加載诚些。如果指定了設(shè)備的版本沒有被發(fā)現(xiàn),那么系統(tǒng)會回過去找原始filename的版本皇型,在例子中就是MyImage.png.