iOS检测更新功能
2019獨角獸企業(yè)重金招聘Python工程師標準>>>
//
// ?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:@"發(fā)現(xiàn)新版本(%@),是否更新", 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
轉載于:https://my.oschina.net/gdxz111/blog/746902
總結
- 上一篇: MySQL中TIMESTAMPDIFF和
- 下一篇: HTTP协议和几种常见的状态码