iOS 版本升级提示功能封装

相关知识点

获取商店应用版本信息

https://itunes.apple.com/cn/lookup?id=appid

具体数据结构

{
    resultCount = 1;
    results =     (
                {
            advisories =             (
            );
            appletvScreenshotUrls =             (
            );
            artistId =;
            artistName = "";
            artistViewUrl = "";
            artworkUrl100 = "";
            artworkUrl512 = "";
            artworkUrl60 = "";
            bundleId = "";
            contentAdvisoryRating = "4+";
            currency = CNY;
            currentVersionReleaseDate = "2018-01-03T23:00:00Z";
            description = "";
            features =             (
            );
            fileSizeBytes = 89503744;
            formattedPrice = "\U514d\U8d39";
            genreIds =             (
                6012
            );
            genres =             (
                "\U751f\U6d3b"
            );
            ipadScreenshotUrls =             (
            );
            isGameCenterEnabled = 0;
            isVppDeviceBasedLicensingEnabled = 1;
            kind = software;
            languageCodesISO2A =             (
                ZH
            );
            minimumOsVersion = "7.1";
            price = 0;
            primaryGenreId = 6012;
            primaryGenreName = Lifestyle;
            releaseDate = "2017-08-15T20:40:34Z";
            releaseNotes = "";
            screenshotUrls =             (
                "",
                "",
                "",
                "",
                ""
            );
            sellerName = "";
            supportedDevices =             (
                "iPhone4-iPhone4",
                "iPad2Wifi-iPad2Wifi",
                "iPad23G-iPad23G",
                "iPhone4S-iPhone4S",
                "iPadThirdGen-iPadThirdGen",
                "iPadThirdGen4G-iPadThirdGen4G",
                "iPhone5-iPhone5",
                "iPodTouchFifthGen-iPodTouchFifthGen",
                "iPadFourthGen-iPadFourthGen",
                "iPadFourthGen4G-iPadFourthGen4G",
                "iPadMini-iPadMini",
                "iPadMini4G-iPadMini4G",
                "iPhone5c-iPhone5c",
                "iPhone5s-iPhone5s",
                "iPadAir-iPadAir",
                "iPadAirCellular-iPadAirCellular",
                "iPadMiniRetina-iPadMiniRetina",
                "iPadMiniRetinaCellular-iPadMiniRetinaCellular",
                "iPhone6-iPhone6",
                "iPhone6Plus-iPhone6Plus",
                "iPadAir2-iPadAir2",
                "iPadAir2Cellular-iPadAir2Cellular",
                "iPadMini3-iPadMini3",
                "iPadMini3Cellular-iPadMini3Cellular",
                "iPodTouchSixthGen-iPodTouchSixthGen",
                "iPhone6s-iPhone6s",
                "iPhone6sPlus-iPhone6sPlus",
                "iPadMini4-iPadMini4",
                "iPadMini4Cellular-iPadMini4Cellular",
                "iPadPro-iPadPro",
                "iPadProCellular-iPadProCellular",
                "iPadPro97-iPadPro97",
                "iPadPro97Cellular-iPadPro97Cellular",
                "iPhoneSE-iPhoneSE",
                "iPhone7-iPhone7",
                "iPhone7Plus-iPhone7Plus",
                "iPad611-iPad611",
                "iPad612-iPad612",
                "iPad71-iPad71",
                "iPad72-iPad72",
                "iPad73-iPad73",
                "iPad74-iPad74",
                "iPhone8-iPhone8",
                "iPhone8Plus-iPhone8Plus",
                "iPhoneX-iPhoneX"
            );
            trackCensoredName = "\U5b81\U5de2\U516c\U5bd3";
            trackContentRating = "4+";
            trackId =;
            trackName = "\U5b81\U5de2\U516c\U5bd3";
            trackViewUrl = "https://itunes.apple.com/cn/app/%E5%AE%81%E5%B7%A2%E5%85%AC%E5%AF%93/id1258999193?mt=8&uo=4";
            version = "1.6.2";
            wrapperType = software;
        }
    );
}

商店下载地址

//app下载链接,改变appid即可
#define APPURL @"https://itunes.apple.com/cn/app/id"APPID@"?mt=8"

获取当前版本

 NSDictionary *infoDic=[[NSBundle mainBundle] infoDictionary];
 NSString *currentVersion = [infoDic valueForKey:@"CFBundleShortVersionString"];

源代码

.h文件

#import 

@interface VersionManager : NSObject

+(void)checkVersion:(UIViewController*)vc;

@end

.m文件

#import "VersionManager.h"

// 苹果商店appid
#define APPID @"1258999193"
//app下载链接
#define APPURL @"https://itunes.apple.com/cn/app/id"APPID@"?mt=8"

@implementation VersionManager

#pragma mark - 检查版本更新

+(void) checkVersion:(UIViewController *)vc {
    __weak UIViewController *weakVC = vc;
    [self checkUpdateWithAppID:APPID success:^(NSDictionary *resultDic, BOOL isNewVersion, NSString *newVersion) {
        if (isNewVersion) {
            [self showUpdateView:newVersion withVC:weakVC];
        }
    } failure:^(NSError *error) {
        
    }];
    
}

+ (void)showUpdateView:(NSString *)newVersion withVC:(UIViewController*)vc
{
    NSString *alertMsg=[[@"宁巢公寓" stringByAppendingString:[NSString stringWithFormat:@"%0.1f",[newVersion floatValue]]] stringByAppendingString:@",赶快体验最新版本吧!"];
    ZLAlertView *alert = [[ZLAlertView alloc] initWithTitle:@"发现新版本" message:alertMsg];
    [alert addBtnTitle:@"取消" action:^{
        
    }];
    [alert addBtnTitle:@"立即升级" action:^{
        NSString *str = APPURL;
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
    }];
    [alert showAlertWithSender:vc];
}

#pragma mark - 检查版本更新
+ (void)checkUpdateWithAppID:(NSString *)appID success:(void (^)(NSDictionary *resultDic , BOOL isNewVersion , NSString * newVersion))success failure:(void (^)(NSError *error))failure
{
    NSString *encodingUrl=[[@"https://itunes.apple.com/cn/lookup?id=" stringByAppendingString:appID] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [HttpRequstData getNoHUDCommonUrl:encodingUrl parameters:nil success:^(id responseObject) {
        NSDictionary *resultDic = responseObject;
        if (![[resultDic objectForKey:@"resultCount"] integerValue]) {
            return;
        }
        NSString * versionStr =[[[resultDic objectForKey:@"results"] objectAtIndex:0] valueForKey:@"version"];
        
//        float version =[versionStr floatValue];
        //self.iTunesLink=[[[resultDic objectForKey:@"results"] objectAtIndex:0] valueForKey:@"trackViewUrl"];
        NSDictionary *infoDic=[[NSBundle mainBundle] infoDictionary];
        NSString *currentVersion = [infoDic valueForKey:@"CFBundleShortVersionString"];
        if([self compareServerVersion:versionStr localVersion:currentVersion]){
            success(resultDic, YES, versionStr);
        }else{
            success(resultDic,NO ,versionStr);
        }
    } failure:^(NSError *error) {
        failure(error);
    }];
    
}

+ (BOOL)compareServerVersion:(NSString*)serverVersion localVersion:(NSString*)localVersion {
    
    //以"."分隔数字然后分配到不同数组
    NSArray * serverArray = [serverVersion componentsSeparatedByString:@"."];
    NSArray * localArray = [localVersion componentsSeparatedByString:@"."];
    
    for (int i = 0; i < serverArray.count; i++) {
        //以服务器版本为基准,判断本地版本位数小于服务器版本时,直接返回(并且判断为新版本,比如服务器1.5.1 本地为1.5)
        if(i > (localArray.count -1)){
            //有新版本,提示!
            return YES;
        }
        //有新版本,服务器版本对应数字大于本地
        if ( [serverArray[i] intValue] > [localArray[i] intValue]) {
            //有新版本,提示!
            return YES;
        }
    }
    return NO;
}

@end

你可能感兴趣的:(iOS 版本升级提示功能封装)