关于提示版本升级~

今天遇到了关于版本升级的问题,弹出提示框给用户提醒升级,一个版本只提醒一次

http://www.jianshu.com/p/62a18e8ed92b   这个是原文,但是有错误 这里改动了一下加了些需求

 

#define kAPP_URL [AppDelegate isLanguageEnglish]?@"http://itunes.apple.com/lookup?id=":@"http://itunes.apple.com/cn/lookup?id="

#define kAppId  @"1111111111"//appID

 

- (void)updateApp

{

    NSError *error;

    

    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:NSJSONReadingMutableContainers 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 *updateVersion = infoDict[@"version"];

    NSString *trackName = infoDict[@"trackName"];

    

    _trackViewUrl = infoDict[@"trackViewUrl"];

    

    //获取当前设备中应用的版本号

    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];

    NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];

    

    NSArray<NSString*> *currentArray = [currentVersion componentsSeparatedByString:@"."];

    NSArray<NSString*> *updateArray = [updateVersion componentsSeparatedByString:@"."];

    

    NSMutableString *currentString = [NSMutableString string];

    for (NSString *str in currentArray) {

        NSString * tempstr = [NSString stringWithFormat:@"%03d",[str intValue]];

        [currentString appendString:tempstr];

    }

    

    NSMutableString *updateString = [NSMutableString string];

    for (NSString *str in updateArray) {

        NSString * tempstr = [NSString stringWithFormat:@"%03d",[str intValue]];//补全位数再比较大小

        [updateString appendString:tempstr];

    }

    if ([[NSUserDefaults standardUserDefaults]objectForKey:updateString]) {

        return;

    }else{

        [[NSUserDefaults standardUserDefaults]setObject:@0 forKey:updateString];//一个版本只提醒一次

    }

    

    //判断两个版本是否相同

    if ([currentString longLongValue] < [updateString longLongValue]) {

        NSString *titleStr = [NSString stringWithFormat:@"检查更新:%@", trackName];

        NSString *messageStr = [NSString stringWithFormat:@"发现新版本 %@ 是否更新\n%@", updateVersion,infoDict[@"releaseNotes"]];//版本更新内容

        

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:titleStr message:messageStr delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"升级", nil];

        

        alert.tag = [kAppId intValue];

        [alert show];

    }

}

//判断用户点击了哪一个按钮

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

    if (alertView.tag == [kAppId intValue]) {

        if (buttonIndex == 1) { //打开app store上应用的详情页面

            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.trackViewUrl]];

        }

    }

}

转载于:https://www.cnblogs.com/huoran1120/p/5859856.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值