NSBundle

来源:互联网 发布:nginx 访问报404 编辑:程序博客网 时间:2024/06/05 00:46

bundle实际上是一个目录,其中包含了程序会使用的资源,包括:图像,声音,编译好的代码,nib文件等,cocoa中对应的类是NSBundle Carbon中对应的是CFBundleRef 

1.获取app的info.plist详细信息

版本号:Bundle version

 NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];

应用标识:Bundle identifier

NSString *bundleId = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"];

应用名称:Bundle display name

NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];

Bundle name

NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"];

 

2.应用程序语言本地化

app本地化宏

#define XLocalizedString(key, comment)        [[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:nil]

中英文两个Localizable.strings文件中键值对,例如

"none" = "确定";
"none" = "none";

宏的用法:(返回NSString *)

localizedString("none", "这是注释")

 

3.获取包内文件路径和文件

获取app包路径

NSString *path = [[NSBundle mainBundle] bundlePath];

app资源目录路径

NSString *resPath = [[NSBundle mainBundle] resourcePath];
 

获取资源目录下a.bundle

NSString* path = [resPath stringByAppendingPathComponent:@"a.bundle"];NSBundle *bundle = [NSBundle bundleWithPath:path];
 

获取app包的readme.txt文件路径

NSString *path = [[NSBundle mainBundle] pathForResource:@"readme" ofType:@"txt"];

NSLog(@"ios 应用发布后 .app 应用文件 路径::%@",[NSBundle mainBundle] );

NSLog(@"ios 应用发布后 .app 应用包(文件) 的详细信息 ::%@",[[NSBundle mainBundle] infoDictionary]);

 

 [[NSBundle mainBundle] infoDictionary] 返回的是一个数据字典内容如下:

(XX1 为当前应用的名字 也就是app 包的名字 )--( 部分地方 XX 大写 我隐藏了部分信息...... )

 ios 应用发布后 .app 应用文件 ::{

    CFBundleDevelopmentRegion = en;

    CFBundleDisplayName = "当前app 文件名XX1";

    CFBundleExecutable = "当前app 文件名XX1";

    CFBundleExecutablePath = "/Users/yq-010/Library/Application Support/iPhone Simulator/5.1/Applications/49419CEE-6608-48FE-81FE-BF40FEEFB8F7/XX1.app/xx1";

    CFBundleIconFiles =     (

        "icon.png",

        "icon@2x.png"

    );

    CFBundleIcons =     {

        CFBundlePrimaryIcon =         {

            CFBundleIconFiles =             (

                "icon.png",

                "icon@2x.png"

            );

            UIPrerenderedIcon = 1;

        };

    };

    CFBundleIdentifier = "com.xxxx.xxxxx";

    CFBundleInfoDictionaryVersion = "6.0";

    CFBundleInfoPlistURL = "Info.plist -- file://localhost/Users/yq-010/Library/Application%20Support/iPhone%20Simulator/5.1/Applications/49419CEE-6608-48FE-81FE-BF40FEEFB8F7/XX%20XX%20XX.app/";

    CFBundleName = "XX1";

    CFBundlePackageType = APPL;

    CFBundleShortVersionString = "1.3";

    CFBundleSignature = "????";

    CFBundleSupportedPlatforms =     (

        iPhoneSimulator

    );

    CFBundleVersion = "1.3";

    DTPlatformName = iphonesimulator;

    DTSDKName = "iphonesimulator5.1";

    LSRequiresIPhoneOS = 1;

    NSBundleInitialPath = "/Users/yq-010/Library/Application Support/iPhone Simulator/5.1/Applications/49419CEE-6608-48FE-81FE-BF40FEEFB8F7/XX1.app";

    NSBundleResolvedPath = "/Users/yq-010/Library/Application Support/iPhone Simulator/5.1/Applications/49419CEE-6608-48FE-81FE-BF40FEEFB8F7/XX1.app";

    UIDeviceFamily =     (

        1

    );

    UIPrerenderedIcon = 1;

    UIRequiredDeviceCapabilities =     (

        armv7

    );

    UISupportedInterfaceOrientations =     (

        UIInterfaceOrientationPortrait

    );

}

 

应用1:我们可以通过包的信息的数据字典 得到 应用的版本 CFBundleVersion = "1.3";

当前应用的版本 :

[[[NSBundlemainBundleinfoDictionaryobjectForKey:@"CFBundleShortVersionString"]]

0 0