iOS-版本更新提示-OC

用到版本更新,首先现在网络上搜了搜发现没有满意的,于是参考一个 重写了一下,支持1.1.1.1.2这种的 版本判断


VersionUpDate.h

#import

#import

//编译版本号

#define CurrentBundleVersion[[[NSBundle mainBundle]infoDictionary]objectForKey:@"CFBundleVersion"]

//更新版本号

#define CurrentVersion[[[NSBundle mainBundle]infoDictionary]objectForKey:@"CFBundleShortVersionString"]

//app名称

#define APPName[[[NSBundle mainBundle]infoDictionary]objectForKey:@"CFBundleDisplayName"]

//检测地址

#define kAppInfoUrl @"http://itunes.apple.com/lookup?id=<#你的上架应用ID#>"

@interface VersionUpdate : NSObject

@property(strong,nonatomic)NSString* downloadUrl;

//发布到appstroe检测

-(void)appVersionCheckWithiTunesAppMark;

+(VersionUpdate *)sharedVersion;

@end


VersionUpDate.m

#import "VersionUpdate.h"

static VersionUpdate *versionUpdate = nil;

@interface VersionUpdate()

@end

@implementation VersionUpdate

+(VersionUpdate *)sharedVersion {

if(versionUpdate == nil){

versionUpdate =[[VersionUpdate alloc]init];

}

return versionUpdate;

}

//发布到appstore版本

-(void)appVersionCheckWithiTunesAppMark{

AFHTTPSessionManager *manager =[AFHTTPSessionManager manager];

[manager POST:kAppInfoUrl parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task,id_Nullable responseObject){

if(responseObject &&[responseObject[@"resultCount"]integerValue])

{

self.downloadUrl=[responseObject[@"results"][0]objectForKey:@"trackViewUrl"];

NSString *lastVersion =[responseObject[@"results"][0]objectForKey:@"version"];

NSMutableArray *shopVersion =[NSMutableArray arrayWithArray:[lastVersion componentsSeparatedByString:@"."]];

NSMutableArray *localVersion =[NSMutableArray arrayWithArray:[CurrentVersion componentsSeparatedByString:@"."]];

NSInteger num = shopVersion.count;

if(num < localVersion.count){

num = localVersion.count;

}

while(shopVersion.count < num){

[shopVersion addObject:@"0"];

}

while(localVersion.count < num){

[localVersion addObject:@"0"];

}

int x = 0;

BOOL newBol = NO;

while(x

if([shopVersion[x]integerValue]>[localVersion[x]integerValue]){

newBol = YES;

}

x ++;

}

if(newBol){

UIAlertView *av =[[UIAlertView alloc]initWithTitle:@"提示"

message:string(@"发现新版本%@,是否下载更新?",lastVersion)

delegate:self

cancelButtonTitle:@"取消"

otherButtonTitles:@"立即更新",nil];

[av show];

}

}

} failure:nil];

}

#pragma mark UIAlertViewDelegate

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {

if(buttonIndex)

{

NSURL *url =[NSURL URLWithString:self.downloadUrl];

[[UIApplication sharedApplication]openURL:url];

}

}

@end


#24  #124

你可能感兴趣的:(iOS-版本更新提示-OC)