1.为 APPDelegate添加 一个 VersonUpdate 分类
2. 在.m 文件中实现方法
//网络请求app的信息
-(void)VersonUpdate{
//定义的app的地址
NSString *urld = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",APPIDForThisApp];
//网络请求app的信息,主要是取得我说需要的Version
NSURL *url = [NSURL URLWithString:urld];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:10];
[request setHTTPMethod:@"POST"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSMutableDictionary *receiveStatusDic=[[NSMutableDictionary alloc]init];
if (data) {
//data是有关于App所有的信息
NSDictionary *receiveDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
if ([[receiveDic valueForKey:@"resultCount"] intValue]>0) {
[receiveStatusDic setValue:@"1" forKey:@"status"];
[receiveStatusDic setValue:[[[receiveDic valueForKey:@"results"] objectAtIndex:0] valueForKey:@"version"] forKey:@"version"];
//请求的有数据,进行版本比较
[self performSelectorOnMainThread:@selector(receiveData:) withObject:receiveStatusDic waitUntilDone:NO];
}else{
[receiveStatusDic setValue:@"-1" forKey:@"status"];
}
}else{
[receiveStatusDic setValue:@"-1" forKey:@"status"];
}
}];
[task resume];
}
//获取自身的版本号并与AppStore比较
-(void)receiveData:(id)sender
{
//获取APP自身版本号
NSString *localVersion = [[[NSBundle mainBundle]infoDictionary]objectForKey:@"CFBundleShortVersionString"];
NSArray *localArray = [localVersion componentsSeparatedByString:@"."];
NSArray *versionArray = [sender[@"version"] componentsSeparatedByString:@"."];
if ((versionArray.count == 3) && (localArray.count == versionArray.count)) {
if ([localArray[0] intValue] < [versionArray[0] intValue]) {
[self updateVersion];
}else if ([localArray[0] intValue] == [versionArray[0] intValue]){
if ([localArray[1] intValue] < [versionArray[1] intValue]) {
[self updateVersion];
}else if ([localArray[1] intValue] == [versionArray[1] intValue]){
if ([localArray[2] intValue] < [versionArray[2] intValue]) {
[self updateVersion];
}
}
}
}
}
//根据比较的结果,实现弹窗
-(void)updateVersion{
NSString *msg = [NSString stringWithFormat:@"更新最新版本,优惠信息提前知"];
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"升级提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction * cancleAction = [UIAlertAction actionWithTitle:@"取消"style:UIAlertActionStyleCancel handler:^(UIAlertAction*action) {
}];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"现在升级"style:UIAlertActionStyleDestructive handler:^(UIAlertAction*action) {
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"itms://itunes.apple.com/us/app/ 开发者名字(一定要英文)/idAPPID?l=zh&ls=1&mt=8"]];
[[UIApplication sharedApplication]openURL:url];
}];
[alertController addAction:cancleAction];
[alertController addAction:okAction];
[self.window.rootViewController presentViewController:alertController animated:YES completion:nil];
}
3. 在.h 文件中暴露接口
//网络请求app的信息
-(void)VersonUpdate;
4. 在 AppDelegate 的调用
-application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法中调用
获取自身的版本号
appStore的版本号
/**/. 跳转 App Store 没有反应
替换中文: