美文网首页ios开发整理
iOS 版本升级提示功能封装

iOS 版本升级提示功能封装

作者: huangxiongbiao | 来源:发表于2018-01-04 12:27 被阅读42次

相关知识点

获取商店应用版本信息

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 <Foundation/Foundation.h>

@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 版本升级提示功能封装

    相关知识点 获取商店应用版本信息 具体数据结构 商店下载地址 获取当前版本 源代码 .h文件 .m文件

  • iOS提示版本升级,仿斗鱼TV---封装

    当你的 app 版本更新之后,一般情况下用户是不会知道的,只有等到 App Store 的图标上有一个大大的"1"...

  • ios提示用户版本升级

    本文主要介绍的是两种提示用户的更新的方法,文字比较多。但都是逻辑 现在苹果审核不能看到版本提示更新的字样以及功能,...

  • iOS 提示用户版本升级

  • ios提示用户版本升级

    本文主要介绍的是两种提示用户的更新的方法,文字比较多。但都是逻辑 现在苹果审核不能看到版本提示更新的字样以及功能,...

  • which may not be supported by th

    真机调试出现提示 原因:手机升级了最新的系统, Xcode 还没升级到最新。 Xcode支持的真机iOS 版本低于...

  • iOS APP提示升级直接跳转到AppStore

    记录一下:当iOS应用有提示升级功能,用户点击【升级】后直接打开AppStore显示该APP界面。 例如淘宝,从i...

  • CocoPods更换Ruby源及升级版本

    升级xcode11,项目适配iOS13,MJExtension报错,github上提示需要更新最新版本,使用终端 ...

  • iOSAPP升级跳转

    目前在做的一个项目在项目末尾,要加上版本升级提示的功能,其功能要点无外乎就是获取App Store上应用的最新版本...

  • iOS13适配中遇到的问题

    又是一年一度的苹果大版本更新啦 最近IOS大版本苹果发布后,由于苹果升级Xcode以及IOS13新功能属性进行变动...

网友评论

    本文标题:iOS 版本升级提示功能封装

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