//
// LBViewController.m
// LiveBroadcast
//
// Created by gz on 16/9/14.
// Copyright © 2016年
//
#import "LBViewController.h"
#define kAPP_URL @"http://itunes.apple.com/lookup?id="
#define kAppID @"112125"
[@interface](https://my.oschina.net/u/996807) LBViewController () {
NSString * _trackViewUrl;
}
[@end](https://my.oschina.net/u/567204)
@implementation LBViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)checkUpdateApp {
NSError *error;
//kAPP_URL : http://itunes.apple.com/lookup?id=
//kAppId : 在iTunes connect上申请的APP ID
NSString *urlStr = [NSString stringWithFormat:@"%@%@", kAPP_URL, kAppID];
NSURL *url = [NSURL URLWithString:urlStr];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSDictionary *appInfoDict = [NSJSONSerialization JSONObjectWithData:response
options:NSJSONReadingAllowFragments
error:&error];
// NSDictionary *appInfoDict = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
if (error) {
NSLog(@"%@", error.description);
return;
}
NSArray *resultArray = [appInfoDict objectForKey:@"results"];
if (![resultArray count]) {
NSLog(@"error : resultArray == nil");
return;
}
NSDictionary *infoDict = [resultArray objectAtIndex:0];
//获取服务器上应用的最新版本号
NSString *appleV = infoDict[@"version"];
NSString *aV = [appleV copy];
NSString *trackName = infoDict[@"trackName"];
_trackViewUrl = infoDict[@"trackViewUrl"];
//获取当前设备中应用的版本号
NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
NSString *cV = [infoDic objectForKey:@"CFBundleShortVersionString"];
cV = [cV stringByReplacingOccurrencesOfString:@"." withString:@""];
aV = [aV stringByReplacingOccurrencesOfString:@"." withString:@""];
NSInteger length = MIN(cV.length, aV.length);
cV = [cV substringToIndex:length];
aV = [aV substringToIndex:length];
CGFloat updateVersion = [aV doubleValue];
CGFloat currentVersion = [cV doubleValue];
//判断两个版本是否相同
if (currentVersion <= updateVersion) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"%@",trackName] message:[NSString stringWithFormat:@"发现新版本(%@),是否更新", appleV] preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}];
[alertController addAction:cancelAction];
UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"升级" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:_trackViewUrl]];
}];
[alertController addAction:sureAction];
[self presentViewController:alertController animated:YES completion:nil];
} else { //版本号和app store上的一致
}
}
@end