[iOS]获取当前项目工程的名称版本等信息

有时候在项目中需要知道当前APP的版本号,项目名称,APP名称,系统版本等信息,这里简单介绍几种方法:


1. 从NSBundle类的infoDictionary中获取,代码如下:

//获取所有信息字典
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    
    //    NSLog(@"%@",infoDictionary);
    //    CFShow((__bridge CFTypeRef)(infoDictionary));
    
    NSString *executableFile = [infoDictionary objectForKey:(NSString *)kCFBundleExecutableKey]; //获取项目名称
    
    NSLog(@"executableFile == %@",executableFile);
    
    NSString *version = [infoDictionary objectForKey:(NSString *)kCFBundleVersionKey]; //获取项目版本号
    NSLog(@"version .. %@",version);
    
    // app名称
    NSString *app_Name = [infoDictionary objectForKey:@"CFBundleDisplayName"];
    NSLog(@"app_Name == %@",app_Name);
    
    // app版本
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    NSLog(@"app_Version .. %@",app_Version);
    // app build版本
    NSString *app_build = [infoDictionary objectForKey:@"CFBundleVersion"];
    NSLog(@"app_build >> %@",app_build);
输出:

2016-03-11 14:40:42.396 PhotosDemo[1930:168508] executableFile == PhotosDemo
2016-03-11 14:40:42.396 PhotosDemo[1930:168508] version .. 1
2016-03-11 14:40:42.396 PhotosDemo[1930:168508] app_Name == 测试
2016-03-11 14:40:42.397 PhotosDemo[1930:168508] app_Version .. 1.0
2016-03-11 14:40:42.397 PhotosDemo[1930:168508] app_build >> 1
具体字典中还有哪些key值,可打印输出后查看,获取所需信息;


2. 使用NSBundle的实例方法objectForInfoDictionaryKey

 NSBundle *mainBundle = [NSBundle mainBundle];
    
    //项目名称
    NSString *executableFile = [mainBundle objectForInfoDictionaryKey:(NSString*)kCFBundleExecutableKey];
    NSLog(@"%@",executableFile);
    
    //版本号
    NSString *version = [mainBundle objectForInfoDictionaryKey:(NSString*)kCFBundleVersionKey];
    NSLog(@"%@",version);
    
    //bundleID
    NSString *bundleID = [mainBundle objectForInfoDictionaryKey:(NSString*)kCFBundleIdentifierKey];
    NSLog(@"%@",bundleID);
    
    //开发语言环境
    NSString *lon = [mainBundle objectForInfoDictionaryKey:(NSString*)kCFBundleDevelopmentRegionKey];
    NSLog(@"%@",lon);
    
    //名称
    NSString *name = [mainBundle objectForInfoDictionaryKey:(NSString*)kCFBundleNameKey];
    NSLog(@"%@",name);
    
    NSString *appName = [mainBundle objectForInfoDictionaryKey:@"CFBundleDisplayName"];
    NSLog(@"%@",appName);

输出:

2016-03-11 14:42:58.076 PhotosDemo[1945:170139] PhotosDemo
2016-03-11 14:42:58.077 PhotosDemo[1945:170139] 1
2016-03-11 14:42:58.077 PhotosDemo[1945:170139] com.artup.PhotosDemo
2016-03-11 14:42:58.077 PhotosDemo[1945:170139] en
2016-03-11 14:42:58.077 PhotosDemo[1945:170139] PhotosDemo
2016-03-11 14:42:58.077 PhotosDemo[1945:170139] 测试

3. 系统中还有一个访问这些属性的方法 CFBundleGetVersionNumber() , CFBundleGetIdentifier() 等方法,他们的参数都是 CFBundleRef 类型的,这个参数从哪获得,确实废了些劲,网上也没找到合适介绍,在翻底层的时候无意间发现 CFBundleRef   CFBundleGetMainBundle( void );这个函数,参数为空,返回一个CFBundleRef;

使用方式如下:

//获取CFBundleRef
    CFBundleRef ref = CFBundleGetMainBundle();
    
    //获取属性字典 同 [[NSBundle mainBundle] infoDictionary]
    NSDictionary *infoDictionary = (NSDictionary*)CFBundleGetInfoDictionary(ref);
    NSLog(@"%@",infoDictionary);
    
    //这个数目比较大,不是我们设置的版本号,在上面的infoDictionary字典中有这个值
    UInt32 versionNumber = CFBundleGetVersionNumber(ref);
    NSLog(@"%ld",(long)versionNumber);
    
    //获取标示符,就是Bundle ID
    NSString *bundleID = (NSString*)CFBundleGetIdentifier(ref);
    NSLog(@"%@",bundleID);

输出:

2016-03-11 14:44:35.150 PhotosDemo[1956:171246] {
    BuildMachineOSBuild = 15D21;
    CFBundleDevelopmentRegion = en;
    CFBundleDisplayName = "\U6d4b\U8bd5";
    CFBundleExecutable = PhotosDemo;
    CFBundleIdentifier = "com.artup.PhotosDemo";
    CFBundleInfoDictionaryVersion = "6.0";
    CFBundleInfoPlistURL = "Info.plist -- file:///Users/mac/Library/Developer/CoreSimulator/Devices/5A7E314D-EC55-4560-A787-BCB1941AE23A/data/Containers/Bundle/Application/0322A01C-45F8-4F60-83FD-B15F30D909F1/PhotosDemo.app/";
    CFBundleName = PhotosDemo;
    CFBundleNumericVersion = 16809984;
    CFBundlePackageType = APPL;
    CFBundleShortVersionString = "1.0";
    CFBundleSignature = "????";
    CFBundleSupportedPlatforms =     (
        iPhoneSimulator
    );
    CFBundleVersion = 1;
    DTCompiler = "com.apple.compilers.llvm.clang.1_0";
    DTPlatformBuild = "";
    DTPlatformName = iphonesimulator;
    DTPlatformVersion = "9.2";
    DTSDKBuild = 13C75;
    DTSDKName = "iphonesimulator9.2";
    DTXcode = 0720;
    DTXcodeBuild = 7C68;
    LSRequiresIPhoneOS = 1;
    MinimumOSVersion = "9.2";
    UIDeviceFamily =     (
        1,
        2
    );
    UILaunchStoryboardName = LaunchScreen;
    UIMainStoryboardFile = Main;
    UIRequiredDeviceCapabilities =     (
        armv7
    );
    UISupportedInterfaceOrientations =     (
        UIInterfaceOrientationPortrait,
        UIInterfaceOrientationLandscapeLeft,
        UIInterfaceOrientationLandscapeRight
    );
}
2016-03-11 14:44:35.150 PhotosDemo[1956:171246] 16809984
2016-03-11 14:44:35.150 PhotosDemo[1956:171246] com.artup.PhotosDemo

另外,系统中预定义了几个常用key:

CF_EXPORT
    const CFStringRef kCFBundleInfoDictionaryVersionKey;
    /* The version of the Info.plist format */
    //Info.plist 文件版本
    
    CF_EXPORT
    const CFStringRef kCFBundleExecutableKey;
    /* The name of the executable in this bundle, if any */
    //可用的项目名称
    
    CF_EXPORT
    const CFStringRef kCFBundleIdentifierKey;
    /* The bundle identifier (for CFBundleGetBundleWithIdentifier()) */
    //项目的bundle identifier
    
    CF_EXPORT
    const CFStringRef kCFBundleVersionKey;
    /* The version number of the bundle.  For Mac OS 9 style version numbers (for example "2.5.3d5"), */
    /* clients can use CFBundleGetVersionNumber() instead of accessing this key directly since that */
    /* function will properly convert the version string into its compact integer representation. */
    //
    CF_EXPORT
    const CFStringRef kCFBundleDevelopmentRegionKey;
    /* The name of the development language of the bundle. */
    //语言环境
    
    CF_EXPORT
    const CFStringRef kCFBundleNameKey;
    /* The human-readable name of the bundle.  This key is often found in the InfoPlist.strings since it is usually localized. */
    
    CF_EXPORT
    const CFStringRef kCFBundleLocalizationsKey;
    /* Allows an unbundled application that handles localization itself to specify which localizations it has available. */


以上只是列举了部分的信息,还有很多信息可以使用相同的方式获取...

你可能感兴趣的:([iOS]获取当前项目工程的名称版本等信息)