IOS之NSBundle方法详解

原文地址:http://blog.csdn.net/shx_yi/article/details/8755415 

1、使用类方法创建一个NSBundler对象

+ (NSBundle *)mainBundle;

eg:[NSBundle mainBundle];

2、使用路径获取一个NSBundle 对象,这个路径应该是一个目录的全路径

+ (NSBundle *)bundleWithPath:(NSString *)path;

eg:

NSString *path = [mailBundle resourcePath];

NSBundle *language = [NSBundlebundleWithPath:path];

3、使用路径初始化一个NSBundle

- (id)initWithPath:(NSString *)path;

4、使用一个url 创建并初始化一个NSBundle对象(这是一个类方法)

注:这里的url 是一个特殊的 文件url路径

+ (NSBundle *)bundleWithURL:(NSURL *)url

5、使用一个url 初始化一个NSBundle对象

注:这里的url 是一个特殊的 文件url路径

- (id)initWithURL:(NSURL *)url

6、根据一个特殊的class 获取NSBundle

+ (NSBundle *)bundleForClass:(Class)aClass;

eg:根据当前的class 获取一个NSBundle // 获取当前类的NSBundle

NSBundle *bud = [NSBundle bundleForClass:[self class]];

NSLog(@"bud==%@",bud);

输出结果如下:

NSBundle

7、获取特定名称的bundle

+ (NSBundle *)bundleWithIdentifier:(NSString *)identifier;

8、使用NSBundle 获取所有的bundle信息(由于ios安全沙盒的限制,所有的获取的资源,是应用程序的资源)

注:官方标注,获取所有的非framework 的bundle;

+ (NSArray *)allBundles;

eg:

NSArray *array = [NSBundle allBundles];

NSLog(@"array===%@",array);

打印的结果如下:

array===(

"NSBundle (loaded)"

)

9、获取应用程序加载的所有framework的资源,

+ (NSArray *)allFrameworks;

eg:

NSArray *array = [NSBundle allFrameworks];

NSLog(@"%@",array);

输出的结果如下:(

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)"

)

10、判断bundle 加载,(按照官方文档,You don’t need to load a bundle’s executablecode to search the bundle’s resources.我们不需要调用这个方法,)

- (BOOL)load;

11、判断bundle 加载

- (BOOL)isLoaded;

12、判断bundle 加载

- (BOOL)unload;

13、加载资源,如果有错误的话,会放置错误信息

- (BOOL)preflightAndReturnError:(NSError **)error

- (BOOL)loadAndReturnError:(NSError **)error

14、获取bundle 类实例的 url

- (NSURL *)bundleURL

15、获取bundle 类实例的 resourceUrl 资源

- (NSURL *)resourceURL

16、获取bundle 类实例的 可执行的URL

- (NSURL *)executableURL

17、(Returns the file URL of the executable with the specified namein the receiver’s bundle.返回一个,文件的URL,使用一个特殊的名称)

- (NSURL *)URLForAuxiliaryExecutable:(NSString*)executableName

18、获取当前NSBundle实例的 URL 资源

- (NSURL *)privateFrameworksURL

19、获取共享的frameworkdURL

- (NSURL *)sharedFrameworksURL

20、 获取支持的Bundle的Url

- (NSURL *)sharedSupportURL

21、获取添加插件的URL

- (NSURL *)builtInPlugInsURL

// 已经不能使用,

- (NSURL *)appStoreReceiptURL

22、 获取bundle 的path 路径

- (NSString *)bundlePath;

23、获取bundle 的资源路径字符串

- (NSString *)resourcePath;

24、获取bundle 可执行文件路径

- (NSString *)executablePath;

25、获取bundle 辅助的path

- (NSString *)pathForAuxiliaryExecutable:(NSString*)executableName;

26、获取私有的路径框架

- (NSString *)privateFrameworksPath;

27、获取共享的framework path 路径

- (NSString *)sharedFrameworksPath;

28、获取共享的路径

- (NSString *)sharedSupportPath;

29、获取插件的路径

- (NSString *)builtInPlugInsPath;

// 已经废弃,不能调用

+ (NSURL *)URLForResource:(NSString *)name withExtension:(NSString*)ext subdirectory:(NSString *)subpath inBundleWithURL:(NSURL*)bundleURL

// 已经废弃

+ (NSArray *)URLsForResourcesWithExtension:(NSString *)extsubdirectory:(NSString *)subpath inBundleWithURL:(NSURL*)bundleURL

30、使用bundle 创建一个资源文件的URL

- (NSURL *)URLForResource:(NSString *)name withExtension:(NSString*)ext

eg:

NSURL* URL=[[NSBundle mainBundle] URLForResource:fileNamewithExtension:@"png"];

31、(官方描述如下:Returns the file URL for the resource file identified bythe specified name and extension and residing in a given bundledirectory.

使用资源文件的名称以及扩展名,还有子路径)

- (NSURL *)URLForResource:(NSString *)name withExtension:(NSString*)ext subdirectory:(NSString *)subpath

32、(官方描述:

Returns the file URL for the resource identified by the specifiedname and file extension,

located in the specified bundle subdirectory, and limited to globalresources and those associated with the specifiedlocalization.)

同上一个方法,不同的是添加了本地资源文件的信息

- (NSURL *)URLForResource:(NSString *)name withExtension:(NSString*)ext subdirectory:(NSString *)subpath localization:(NSString*)localizationName

33、根据文件的后缀名称和子目录,获取一个NSURL 的数组

- (NSArray *)URLsForResourcesWithExtension:(NSString *)extsubdirectory:(NSString *)subpath

34、同上面的方法,添加了本地化的一个资源文件

- (NSArray *)URLsForResourcesWithExtension:(NSString *)extsubdirectory:(NSString *)subpath localization:(NSString*)localizationName

35、根据资源文件的名称,或者是文件的后缀名称以及目录的路径,获取 path

+ (NSString *)pathForResource:(NSString *)name ofType:(NSString*)ext inDirectory:(NSString *)bundlePath;

36、根据文件的扩展名,以及资源的路径,获取一个数组

+ (NSArray *)pathsForResourcesOfType:(NSString *)extinDirectory:(NSString *)bundlePath;

37、根据文件的名称和扩展名获取 path 名称

- (NSString *)pathForResource:(NSString *)name ofType:(NSString*)ext;

37、根据文件的名称和扩展名获取 path 名称

- (NSString *)pathForResource:(NSString *)name ofType:(NSString*)ext inDirectory:(NSString *)subpath;

37、根据文件的名称和扩展名获取 path 名称

- (NSString *)pathForResource:(NSString *)name ofType:(NSString*)ext inDirectory:(NSString *)subpath forLocalization:(NSString*)localizationName;

38、根据文件的扩展名和子目录获取一个资源文件的数组

- (NSArray *)pathsForResourcesOfType:(NSString *)extinDirectory:(NSString *)subpath;

39、同上,添加了资源文件的一个路径

- (NSArray *)pathsForResourcesOfType:(NSString *)extinDirectory:(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/iPhoneSimulator/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/ApplicationSupport/iPhoneSimulator/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/iPhoneSimulator/6.1/Applications/2F3DA58F-5CF9-48A2-ADB2-C923A29B519E/IOS_Example_NSBundle.app";

NSBundleResolvedPath ="/Users/ctrip1/Library/Application Support/iPhoneSimulator/6.1/Applications/2F3DA58F-5CF9-48A2-ADB2-C923A29B519E/IOS_Example_NSBundle.app";

UIDeviceFamily =    (

1,

2

);

UIRequiredDeviceCapabilities =    (

armv7

);

UISupportedInterfaceOrientations =    (

UIInterfaceOrientationPortrait,

UIInterfaceOrientationLandscapeLeft,

UIInterfaceOrientationLandscapeRight

);

}

41、返回本地化资源的NSDictionary 对象

- (NSDictionary *)localizedInfoDictionary;

42、根据key 值获取本地化资源对象的值

- (id)objectForInfoDictionaryKey:(NSString *)key;

43、(官方描述:Returns the Class object for the specifiedname.根据类名字符串获取一个类对象)

- (Class)classNamed:(NSString *)className;

44、返回主要的类

- (Class)principalClass;

45、返回本地化资源的列表

- (NSArray *)localizations;

46、本地化的语言列表

- (NSArray *)preferredLocalizations;

47、使用创建的类获取本地化语言

- (NSString *)developmentLocalization;

48、(官方描述 Returns one or more localizations from the specified listthat a bundle object would use to locate resources for the currentuser.)

+ (NSArray *)preferredLocalizationsFromArray:(NSArray*)localizationsArray;

+ (NSArray *)preferredLocalizationsFromArray:(NSArray*)localizationsArray forPreferences:(NSArray *)preferencesArray;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,050评论 4 370
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,538评论 1 306
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 111,673评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,622评论 0 218
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,047评论 3 295
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,974评论 1 224
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,129评论 2 317
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,893评论 0 209
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,654评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,828评论 2 254
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,297评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,619评论 3 262
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,326评论 3 243
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,176评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,975评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,118评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,909评论 2 278

推荐阅读更多精彩内容