ios零碎记录111

iOS版本更新比较及广告页展示判断

2016-03-04  本文已影响499人  跑调的安眠曲

更新后版本比较

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

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

NSString+VersionCompare.h文件

#import <Foundation/Foundation.h>

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];
上一篇下一篇

猜你喜欢

热点阅读