IOS_基础类库之NSBundle

来源:互联网 发布:如何定制淘宝客app 编辑:程序博客网 时间:2024/05/20 15:40
1、使用类方法创建一个NSBundler对象
+ (NSBundle *)mainBundle;
eg:[NSBundle mailBundle];
2、使用路径获取一个NSBundle 对象,这个路径应该是一个目录的全路径
+ (NSBundle *)bundleWithPath:(NSString *)path;
eg:  
NSString *path = [mailBundle resourcePath];
  NSBundle *language = [NSBundle bundleWithPath: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 </Users/ctrip1/Library/Application Support/iPhone Simulator/6.1/Applications/2F3DA58F-5CF9-48A2-ADB2-C923A29B519E/IOS_Example_NSBundle.app>
7、获取特定名称的bundle
+ (NSBundle *)bundleWithIdentifier:(NSString *)identifier;
8、使用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)"
)
9、获取应用程序加载的所有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)",
   "NSBundle </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/PrivateFrameworks/JavaScriptCore.framework> (loaded)",
   "NSBundle </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/PrivateFrameworks/AggregateDictionary.framework> (loaded)",
   "NSBundle </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/PrivateFrameworks/WebCore.framework> (loaded)",
   "NSBundle </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/PrivateFrameworks/SpringBoardServices.framework> (loaded)",
   "NSBundle </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/PrivateFrameworks/UIFoundation.framework> (loaded)",
   "NSBundle </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/PrivateFrameworks/DictionaryServices.framework> (loaded)",
   "NSBundle </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/usr/lib> (loaded)",
   "NSBundle </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/Frameworks/Foundation.framework> (loaded)",
   "NSBundle </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/PrivateFrameworks/IMFoundation.framework> (loaded)",
   "NSBundle </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/Frameworks/QuartzCore.framework> (loaded)",
   "NSBundle </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/Frameworks/CoreVideo.framework> (loaded)",
   "NSBundle </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/Frameworks/OpenGLES.framework> (loaded)",
   "NSBundle </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/PrivateFrameworks/WebBookmarks.framework> (loaded)",
   "NSBundle </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/Frameworks/CoreFoundation.framework> (loaded)",
   "NSBundle </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)",
   "NSBundle </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/PrivateFrameworks/TelephonyUtilities.framework> (loaded)",
   "NSBundle </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/Frameworks/CFNetwork.framework> (loaded)",
   "NSBundle </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/Frameworks/CoreText.framework> (loaded)",
   "NSBundle </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/PrivateFrameworks/WebKit.framework> (loaded)",
   "NSBundle </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/PrivateFrameworks/MobileAsset.framework> (loaded)",
   "NSBundle </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/Frameworks/CoreTelephony.framework> (loaded)"
)


10、判断bundle 加载,(按照官方文档,You don’t need to load a bundle’s executable code 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 name in 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 *)ext subdirectory:(NSString *)subpath inBundleWithURL:(NSURL *)bundleURL


30、使用bundle 创建一个资源文件的URL
- (NSURL *)URLForResource:(NSString *)name withExtension:(NSString *)ext
eg:
NSURL* URL=[[NSBundle mainBundle] URLForResource:fileName withExtension:@"png"];
31、(官方描述如下: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 *)subpath


32、(官方描述:
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 *)localizationName


33、根据文件的后缀名称和子目录,获取一个NSURL 的数组
- (NSArray *)URLsForResourcesWithExtension:(NSString *)ext subdirectory:(NSString *)subpath
34、同上面的方法,添加了本地化的一个资源文件
- (NSArray *)URLsForResourcesWithExtension:(NSString *)ext subdirectory:(NSString *)subpath localization:(NSString *)localizationName


35、根据资源文件的名称,或者是文件的后缀名称以及目录的路径,获取 path
+ (NSString *)pathForResource:(NSString *)name ofType:(NSString *)ext inDirectory:(NSString *)bundlePath;


36、根据文件的扩展名,以及资源的路径,获取一个数组
+ (NSArray *)pathsForResourcesOfType:(NSString *)ext inDirectory:(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;


- (NSArray *)pathsForResourcesOfType:(NSString *)ext inDirectory:(NSString *)subpath;
- (NSArray *)pathsForResourcesOfType:(NSString *)ext inDirectory:(NSString *)subpath forLocalization:(NSString *)localizationName;


- (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName NS_FORMAT_ARGUMENT(1);


// 返回当前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
   );
}


- (NSDictionary *)localizedInfoDictionary;
- (id)objectForInfoDictionaryKey:(NSString *)key;
- (Class)classNamed:(NSString *)className;
- (Class)principalClass;


- (NSArray *)localizations;
- (NSArray *)preferredLocalizations;
- (NSString *)developmentLocalization;


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


enum {
    NSBundleExecutableArchitectureI386      = 0x00000007,
    NSBundleExecutableArchitecturePPC       = 0x00000012,
    NSBundleExecutableArchitectureX86_64    = 0x01000007,
    NSBundleExecutableArchitecturePPC64     = 0x01000012
};


- (NSArray *)executableArchitectures NS_AVAILABLE(10_5, 2_0);


@end


#define NSLocalizedString(key, comment) \
   [[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:nil]
#define NSLocalizedStringFromTable(key, tbl, comment) \
   [[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:(tbl)]
#define NSLocalizedStringFromTableInBundle(key, tbl, bundle, comment) \
   [bundle localizedStringForKey:(key) value:@"" table:(tbl)]
#define NSLocalizedStringWithDefaultValue(key, tbl, bundle, val, comment) \
   [bundle localizedStringForKey:(key) value:(val) table:(tbl)]


FOUNDATION_EXPORT NSString * const NSBundleDidLoadNotification;
FOUNDATION_EXPORT NSString * const NSLoadedClasses;// notification key

原创粉丝点击