一、使用類方法建立一個NSBundler對象+ (NSBundle *)mainBundle;eg:[NSBundle mailBundle];二、使用路徑獲取一個NSBundle 對象,這個路徑應該是一個目錄的全路徑+ (NSBundle *)bundleWithPath:(NSString *)path;eg: NSString *path = [mailBundle resourcePath]; NSBundle *language = [NSBundle bundleWithPath:path];三、使用路徑初始化一個NSBundle- (id)initWithPath:(NSString *)path;四、使用一個url 建立並初始化一個NSBundle對象(這是一個類方法)注:這裏的url 是一個特殊的 文件url路徑+ (NSBundle *)bundleWithURL:(NSURL *)url五、使用一個url 初始化一個NSBundle對象注:這裏的url 是一個特殊的 文件url路徑- (id)initWithURL:(NSURL *)url六、根據一個特殊的class 獲取NSBundle+ (NSBundle *)bundleForClass:(Class)aClass;eg:根據當前的class 獲取一個NSBundle // 獲取當前類的NSBundleNSBundle *bud = [NSBundle bundleForClass:[self class]];NSLog(@"bud==%@",bud);輸出結果以下:NSBundle </Users/ctrip1/Library/Application Support/iPhone Simulator/6.1/Applications/2F3DA58F-5CF9-48A2-ADB2-C923A29B519E/IOS_Example_NSBundle.app>七、獲取特定名稱的bundle+ (NSBundle *)bundleWithIdentifier:(NSString *)identifier;八、使用NSBundle 獲取全部的bundle信息(因爲ios安全沙盒的限制,全部的獲取的資源,是應用程序的資源)注:官方標註,獲取全部的非framework 的bundle;+ (NSArray *)allBundles;eg:NSArray *array = [NSBundle allBundles];NSLog(@"array===%@",array);打印的結果以下:array===( "NSBundle </Users/ctrip1/Library/Application Support/iPhone Simulator/6.1/Applications/2F3DA58F-5CF9-48A2-ADB2-C923A29B519E/IOS_Example_NSBundle.app> (loaded)")九、獲取應用程序加載的全部framework的資源,+ (NSArray *)allFrameworks;eg:NSArray *array = [NSBundle allFrameworks];NSLog(@"%@",array);輸出的結果以下:( "NSBundle </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/PrivateFrameworks/XPCObjects.framework> (loaded)", </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/PrivateFrameworks/ProofReader.framework> (loaded)", "NSBundle </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/PrivateFrameworks/CommonUtilities.framework> (loaded)", "NSBundle </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/PrivateFrameworks/PrintKit.framework> (loaded)", "NSBundle </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/Frameworks/MobileCoreServices.framework> (loaded)", "NSBundle </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/usr/lib/system> (loaded)", "NSBundle </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/Frameworks/UIKit.framework> (loaded)", "NSBundle </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/PrivateFrameworks/DataMigration.framework> (loaded)", "NSBundle </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/PrivateFrameworks/FaceCoreLight.framework> (loaded)", "NSBundle </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/PrivateFrameworks/AppSupport.framework> (loaded)", "NSBundle </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/PrivateFrameworks/AssetsLibraryServices.framework> (loaded)", "NSBundle </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/PrivateFrameworks/BackBoardServices.framework> (loaded)", "NSBundle </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/Frameworks/CoreImage.framework> (loaded)", "NSBundle </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/PrivateFrameworks/MobileInstallation.framework> (loaded)",) 十、判斷bundle 加載,(按照官方文檔,You don’t need to load a bundle’s executable code to search the bundle’s resources.咱們不須要調用這個方法,)- (BOOL)load;十一、判斷bundle 加載- (BOOL)isLoaded;十二、判斷bundle 加載- (BOOL)unload;1三、加載資源,若是有錯誤的話,會放置錯誤信息- (BOOL)preflightAndReturnError:(NSError **)error- (BOOL)loadAndReturnError:(NSError **)error1四、獲取bundle 類實例的 url - (NSURL *)bundleURL1五、獲取bundle 類實例的 resourceUrl 資源- (NSURL *)resourceURL1六、獲取bundle 類實例的 可執行的URL - (NSURL *)executableURL1七、(Returns the file URL of the executable with the specified name in the receiver’s bundle.返回一個,文件的URL,使用一個特殊的名稱)- (NSURL *)URLForAuxiliaryExecutable:(NSString *)executableName1八、獲取當前NSBundle實例的 URL 資源- (NSURL *)privateFrameworksURL1九、獲取共享的frameworkdURL- (NSURL *)sharedFrameworksURL20、 獲取支持的Bundle的Url- (NSURL *)sharedSupportURL2一、獲取添加插件的URL - (NSURL *)builtInPlugInsURL// 已經不能使用,- (NSURL *)appStoreReceiptURL2二、 獲取bundle 的path 路徑- (NSString *)bundlePath;2三、獲取bundle 的資源路徑字符串- (NSString *)resourcePath;2四、獲取bundle 可執行文件路徑- (NSString *)executablePath;2五、獲取bundle 輔助的path- (NSString *)pathForAuxiliaryExecutable:(NSString *)executableName;2六、獲取私有的路徑框架- (NSString *)privateFrameworksPath;2七、獲取共享的framework path 路徑- (NSString *)sharedFrameworksPath;2八、獲取共享的路徑- (NSString *)sharedSupportPath;2九、獲取插件的路徑- (NSString *)builtInPlugInsPath;// 已經廢棄,不能調用+ (NSURL *)URLForResource:(NSString *)name withExtension:(NSString *)ext subdirectory:(NSString *)subpath inBundleWithURL:(NSURL *)bundleURL// 已經廢棄+ (NSArray *)URLsForResourcesWithExtension:(NSString *)ext subdirectory:(NSString *)subpath inBundleWithURL:(NSURL *)bundleURL30、使用bundle 建立一個資源文件的URL- (NSURL *)URLForResource:(NSString *)name withExtension:(NSString *)exteg:NSURL* URL=[[NSBundle mainBundle] URLForResource:fileName withExtension:@"png"];3一、(官方描述以下:Returns the file URL for the resource file identified by the specified name and extension and residing in a given bundle directory.使用資源文件的名稱以及擴展名,還有子路徑)- (NSURL *)URLForResource:(NSString *)name withExtension:(NSString *)ext subdirectory:(NSString *)subpath3二、(官方描述: Returns the file URL for the resource identified by the specified name and file extension, located in the specified bundle subdirectory, and limited to global resources and those associated with the specified localization.)同上一個方法,不一樣的是添加了本地資源文件的信息- (NSURL *)URLForResource:(NSString *)name withExtension:(NSString *)ext subdirectory:(NSString *)subpath localization:(NSString *)localizationName3三、根據文件的後綴名稱和子目錄,獲取一個NSURL 的數組- (NSArray *)URLsForResourcesWithExtension:(NSString *)ext subdirectory:(NSString *)subpath3四、同上面的方法,添加了本地化的一個資源文件- (NSArray *)URLsForResourcesWithExtension:(NSString *)ext subdirectory:(NSString *)subpath localization:(NSString *)localizationName3五、根據資源文件的名稱,或者是文件的後綴名稱以及目錄的路徑,獲取 path+ (NSString *)pathForResource:(NSString *)name ofType:(NSString *)ext inDirectory:(NSString *)bundlePath;3六、根據文件的擴展名,以及資源的路徑,獲取一個數組+ (NSArray *)pathsForResourcesOfType:(NSString *)ext inDirectory:(NSString *)bundlePath;3七、根據文件的名稱和擴展名獲取 path 名稱- (NSString *)pathForResource:(NSString *)name ofType:(NSString *)ext;3七、根據文件的名稱和擴展名獲取 path 名稱- (NSString *)pathForResource:(NSString *)name ofType:(NSString *)ext inDirectory:(NSString *)subpath;3七、根據文件的名稱和擴展名獲取 path 名稱- (NSString *)pathForResource:(NSString *)name ofType:(NSString *)ext inDirectory:(NSString *)subpath forLocalization:(NSString *)localizationName;3八、根據文件的擴展名和子目錄獲取一個資源文件的數組- (NSArray *)pathsForResourcesOfType:(NSString *)ext inDirectory:(NSString *)subpath;3九、同上,添加了資源文件的一個路徑- (NSArray *)pathsForResourcesOfType:(NSString *)ext inDirectory:(NSString *)subpath forLocalization:(NSString *)localizationName;40、方法調用解釋以下- (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName NS_FORMAT_ARGUMENT(1);你能夠經過NSBundle來找到對應key值的vaule.NSBundle *main = [NSBundle mainBundle];NSString *aString = [main localizedStringForKey:@"Key1" value:@"DefaultValue1" table:@"Find"];上面的代碼會在Find.strings中查找"Key1"對應的vuale字符串. 若是沒有提供用戶指定的語言的本地化資源,那麼就會查找第二個所選語言,若是第二個也沒有本地化資源,就依次找下去. 若是到最後仍是沒有找到,那麼 ""DefaultValue1"將會返回// 返回當前bundle的惟一標示:(即:應用的惟一標示)- (NSString *)bundleIdentifier;eg:com.company.ios-Example-NSBundle.IOS-Example-NSBundle// 獲取資源文件的dictionary 對象- (NSDictionary *)infoDictionary;eg:{ CFBundleDevelopmentRegion = en; CFBundleDisplayName = "IOS_Example_NSBundle"; CFBundleExecutable = "IOS_Example_NSBundle"; CFBundleExecutablePath = "/Users/ctrip1/Library/Application Support/iPhone Simulator/6.1/Applications/2F3DA58F-5CF9-48A2-ADB2-C923A29B519E/IOS_Example_NSBundle.app/IOS_Example_NSBundle"; CFBundleIdentifier = "com.company.ios-Example-NSBundle.IOS-Example-NSBundle"; CFBundleInfoDictionaryVersion = "6.0"; CFBundleInfoPlistURL = "Info.plist -- file://localhost/Users/ctrip1/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/2F3DA58F-5CF9-48A2-ADB2-C923A29B519E/IOS_Example_NSBundle.app/"; CFBundleName = "IOS_Example_NSBundle"; CFBundlePackageType = APPL; CFBundleShortVersionString = "1.0"; CFBundleSignature = "????"; CFBundleSupportedPlatforms = ( iPhoneSimulator ); CFBundleVersion = "1.0"; DTPlatformName = iphonesimulator; DTSDKName = "iphonesimulator6.1"; LSRequiresIPhoneOS = 1; NSBundleInitialPath = "/Users/ctrip1/Library/Application Support/iPhone Simulator/6.1/Applications/2F3DA58F-5CF9-48A2-ADB2-C923A29B519E/IOS_Example_NSBundle.app"; NSBundleResolvedPath = "/Users/ctrip1/Library/Application Support/iPhone Simulator/6.1/Applications/2F3DA58F-5CF9-48A2-ADB2-C923A29B519E/IOS_Example_NSBundle.app"; UIDeviceFamily = ( 1, 2 ); UIRequiredDeviceCapabilities = ( armv7 ); UISupportedInterfaceOrientations = ( UIInterfaceOrientationPortrait, UIInterfaceOrientationLandscapeLeft, UIInterfaceOrientationLandscapeRight );}4一、返回本地化資源的NSDictionary 對象- (NSDictionary *)localizedInfoDictionary;4二、根據key 值獲取本地化資源對象的值- (id)objectForInfoDictionaryKey:(NSString *)key;4三、(官方描述:Returns the Class object for the specified name.根據類名字符串獲取一個類對象)- (Class)classNamed:(NSString *)className;4四、返回主要的類- (Class)principalClass;4五、返回本地化資源的列表- (NSArray *)localizations;4六、本地化的語言列表- (NSArray *)preferredLocalizations;4七、使用建立的類獲取本地化語言- (NSString *)developmentLocalization;4八、(官方描述 Returns one or more localizations from the specified list that a bundle object would use to locate resources for the current user.)+ (NSArray *)preferredLocalizationsFromArray:(NSArray *)localizationsArray;+ (NSArray *)preferredLocalizationsFromArray:(NSArray *)localizationsArray forPreferences:(NSArray *)preferencesArray;
https://www.cnblogs.com/tiffany-my/p/5315028.htmlhtml
MJRefresh中關於NSBundle的簡單使用ios
@implementation NSBundle (MJRefresh)數組
+ (instancetype)mj_refreshBundle安全
{app
static NSBundle *refreshBundle = nil;框架
if (refreshBundle == nil) {iphone
// 這裏不使用mainBundle是爲了適配pod 1.x和0.xide
refreshBundle = [NSBundle bundleWithPath:[[NSBundle bundleForClass:[MJRefreshComponent class]] pathForResource:@"MJRefresh" ofType:@"bundle"]];post
}ui
return refreshBundle;
}
+ (UIImage *)mj_arrowImage
{
static UIImage *arrowImage = nil;
if (arrowImage == nil) {
arrowImage = [[UIImage imageWithContentsOfFile:[[self mj_refreshBundle] pathForResource:@"arrow@2x" ofType:@"png"]] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
}
return arrowImage;
}
+ (NSString *)mj_localizedStringForKey:(NSString *)key
{
return [self mj_localizedStringForKey:key value:nil];
}
+ (NSString *)mj_localizedStringForKey:(NSString *)key value:(NSString *)value
{
static NSBundle *bundle = nil;
if (bundle == nil) {
// (iOS獲取的語言字符串比較不穩定)目前框架只處理en、zh-Hans、zh-Hant三種狀況,其餘按照系統默認處理
NSString *language = [NSLocale preferredLanguages].firstObject;
if ([language hasPrefix:@"en"]) {
language = @"en";
} else if ([language hasPrefix:@"zh"]) {
if ([language rangeOfString:@"Hans"].location != NSNotFound) {
language = @"zh-Hans"; // 簡體中文
} else { // zh-Hant\zh-HK\zh-TW
language = @"zh-Hant"; // 繁體中文
}
} else {
language = @"en";
}
// 從MJRefresh.bundle中查找資源
bundle = [NSBundle bundleWithPath:[[NSBundle mj_refreshBundle] pathForResource:language ofType:@"lproj"]];
}
value = [bundle localizedStringForKey:key value:value table:nil];
return [[NSBundle mainBundle] localizedStringForKey:key value:value table:nil];
}
@end
ZFPlayer中的使用
// 圖片路徑
#define ZFPlayerSrcName(file) [@"ZFPlayer.bundle" stringByAppendingPathComponent:file]
#define ZFPlayerFrameworkSrcName(file) [@"Frameworks/ZFPlayer.framework/ZFPlayer.bundle" stringByAppendingPathComponent:file]
#define ZFPlayerImage(file) [UIImage imageNamed:ZFPlayerSrcName(file)] ? :[UIImage imageNamed:ZFPlayerFrameworkSrcName(file)]