博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS版本更新比较及广告页展示判断
阅读量:6998 次
发布时间:2019-06-27

本文共 3267 字,大约阅读时间需要 10 分钟。

更新后版本比较

      目前app启动之后都会进行版本比较,虽然apple审核规则中明确指出自动检测版本的app将会被拒绝,但是很多app种仍然保持着app版本的检测,然后提供一个按钮跳转到app store去更新版本,具体做法应该是通过后台接口来控制是否开启检测,审核时关闭该功能,审核通过后,开启该功能。

鉴于NSString有提供compare的功能,但是会对于[@"10.1.1" compare:@"2.1.1"]会出现问题,所以提供以下方法进行比较:

NSString+VersionCompare.h文件

#import 
typedef enum : NSUInteger {    BiggerResultType,    SmallerResultType,    EqualResultType} CompareResultType;@interface NSString (VersionCompare)#pragma mark - 与老版本比较- (CompareResultType)versionCompare:(NSString *)oldVersion;@end复制代码

NSString+VersionCompare.m文件

#import "NSString+VersionCompare.h"@implementation NSString (VersionCompare)#pragma mark - 与老版本比较- (CompareResultType)versionCompare:(NSString *)oldVersion {    NSArray *currentVersionArray = [self componentsSeparatedByString:@"."];    NSArray *oldVersionArray = [oldVersion componentsSeparatedByString:@"."];    NSInteger currentVersionLength = currentVersionArray.count;    NSInteger oldVersionLength = oldVersionArray.count;        //小数点个数不相等,按下标索引从小到大比较,若较小的位数都相同,那么较大位数的大(1.1.1 > 1.1)    if (currentVersionLength != oldVersionLength) {        NSInteger smallerLength = currentVersionLength < oldVersionLength ? currentVersionLength : oldVersionLength;        for (int i = 0; i < smallerLength; i++) {            if ([currentVersionArray[i] intValue] < [oldVersionArray[i] intValue]) {                return SmallerResultType;            } else if ([currentVersionArray[i] intValue] > [oldVersionArray[i] intValue]) {                return BiggerResultType;            } else {                continue; //相等就继续            }        }        //前面的都相等, (1.1.1 > 1.1)        return currentVersionLength < oldVersionLength ? SmallerResultType : BiggerResultType;    } else {       //小数点个数相等,按下标索引从小到大比较        for (int i = 0; i < currentVersionLength; i++) {            if ([currentVersionArray[i] intValue] < [oldVersionArray[i] intValue]) {                return SmallerResultType;            } else if ([currentVersionArray[i] intValue] > [oldVersionArray[i] intValue]) {                return BiggerResultType;            } else {                continue; //相等就继续            }        }    }    return EqualResultType;}@end复制代码

使用方法:

//引入头文件#import "NSString+VersionCompare.h" //使用[@"1.1" versionCompare:@"1.1.1"];  //小    [@"10.2.0" versionCompare:@"2.2.0"];  //大    [@"1.2.0" versionCompare:@"1.2.1"];  //小    [@"1.1" versionCompare:@"1.1"]; //等复制代码

新版本广告展示判断

     目前很多app在版本更新的时候,都会有更新内容提醒,具体操作办法如下:

//1.获取缓存的版本号    NSString *softwareVersion = [[NSUserDefaults standardUserDefaults] objectForKey:kSoftwareVersion];    //2.获取当前的版本号    NSString *currentVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];        if (![currentVersion isEqualToString:softwareVersion]) {//新版本更新        [[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:kSoftwareVersion];        [[NSUserDefaults standardUserDefaults] synchronize];        //可能要营销的广告视图            }复制代码
补充:

1.获取系统版本号:

NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];复制代码

2.跳转到app store详情页

NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com/app/id你的appID"];[[UIApplication sharedApplication] openURL:url];复制代码

转载于:https://juejin.im/post/5a3c80d5f265da4320036e0a

你可能感兴趣的文章
93. [NOIP2001] 数的划分
查看>>
c++友元实现操作符重载
查看>>
LeetCode_Maximum Depth of Binary Tree
查看>>
MongoDB入门学习(一):MongoDB的安装和管理
查看>>
beans.factory.BeanCreationException beans.factory.annotation.Autowired(required=true)
查看>>
grep常见使用方法总结
查看>>
视频云的选型调研
查看>>
MySQL 性能调优的10个方法
查看>>
http协议的再次理解
查看>>
Android 利用Gson生成或解析json
查看>>
License友好的前端组件合集
查看>>
OCR 基本知识
查看>>
Oracle中对数字加汉字的排序(完好)
查看>>
Redis具体解释
查看>>
thinkphp中cookie和session中操作数组的方法
查看>>
rman备份OBSOLETE和EXPIRED参数来历及区别
查看>>
NewLife.Redis基础教程
查看>>
BlockingQueue(阻塞队列)详解
查看>>
Hystrix快速入门
查看>>
十大励志电影
查看>>