美文网首页iOS Developer
获取应用版本信息等

获取应用版本信息等

作者: 村长大人tardis_cxx | 来源:发表于2016-12-12 14:07 被阅读175次

标签: iOS 技术


在App应用中,我们经常会迭代App,而需要添加当前版本的新特性,那么就需要获取用户已安装App的版本信息,如何获取呢?请看代码:

// 获得版本号
NSString *currentVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];

在版本对比时,切记不能转换成float或者double类型去比较,而应该用OC方法,如代码:

NSComparisonResult *result = [currentVersion compare:shortVersion];
// NSComparisonResult 是一个枚举,有3种状态,通过状态我们就可以得出App当前版本和服务器版本高低
    typedef NS_ENUM(NSInteger, NSComparisonResult) {NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending};

上面的比较方法会返回一个枚举,我们可以封装一个返回值类型为BOOL方法,返回值就根据枚举状态来判断
获取其他应用信息

// 两个获得应用信息的方法
    [[[NSBundle mainBundle] infoDictionary] valueForKey:@"key"];
    [[NSBundle mainBundle] objectForInfoDictionaryKey:@"key"];
    
    // 获得应用名
    NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"];
    // 获得版本号
    NSString *shortVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
    // 获得build号
    NSString *buildViersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
    // 获得bundle id
    NSString *dundleId = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleIdentifier"];
    // 获得应用自定义名称
    NSString *displayName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];
    
    NSLog(@"\n appName = %@ \n shortVersion = %@ \n buildViersion = %@ \n dundleId = %@ \n displayName = %@", appName, shortVersion, buildViersion, dundleId, displayName);

以上就是通过key来获取app版本信息的方法

相关文章

网友评论

    本文标题:获取应用版本信息等

    本文链接:https://www.haomeiwen.com/subject/ikpmmttx.html