iOS检测更新功能

本文介绍了一个iOS应用中实现的应用更新检查功能。通过访问iTunes API,该方法能够获取到应用程序的最新版本信息,并与本地安装的版本进行比较,从而决定是否提示用户进行更新。

//
//  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

转载于:https://my.oschina.net/gdxz111/blog/746902

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值