IOS 新增强制更新或者推荐更新流程

cocos2d-x 项目中需要在游戏里提示版本有更新

代码如下,在Oc的主函数中新增(本人的为AppController.mm)

 //app强制更新
    //定义的app的地址
    NSString *urld = [NSString stringWithFormat:@" http://itunes.apple.com/cn/lookup?id=%@ 

",@"你的appleID"];   //这里注意:链接不要动,这是苹果官方给出的接口,返回Json格式的版本信息
    
    //网络请求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) {
            //[NSThread sleepForTimeInterval:1.0];
            //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];

新增两个函数

 -(void)updateVersion{
    NSString *msg = [NSString stringWithFormat:@"又出新版本啦,快点更新吧!"];
   // [NSThread sleepForTimeInterval:1.0];   //这个可以不用
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"升级提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
    
    // Create the actions.
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"下次再说" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"现在升级" style:UIAlertActionStyleDestructive handler:^(UIAlertAction*action) {
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"App Store下载链接"]];
        [[UIApplication sharedApplication]openURL:url];
    }];
    
    // Add the actions.
    [alertController addAction:cancelAction];
    [alertController addAction:otherAction];
    
    [self.window.rootViewController presentViewController:alertController animated:YES completion:nil];
    
}

-(void)receiveData:(id)sender
{
    //获取APP自身版本号
    NSString *localVersion = [[[NSBundle mainBundle]infoDictionary]objectForKey:@"CFBundleShortVersionString"];
    
    NSArray *localArray = [localVersion componentsSeparatedByString:@"."];
    NSArray *versionArray = [sender[@"version"] componentsSeparatedByString:@"."];
    //版本号比较
    if(localArray.count == 2 && versionArray.count == 2)
    {
        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];
            }
        }
    }
    if(localArray.count == 2 && versionArray.count == 3)
    {
        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([versionArray[2] intValue] > 0)
                {
                    [self updateVersion];
                }
            }
        }
    }
    if(localArray.count == 3 && versionArray.count == 2)
    {
        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];
            }
        }
    }
    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];
                }
            }
        }
    }
}

然后就好啦!启动就会有提示更新的啦
版本号支持到3位,比如2.5.17这样的,4位的自己新增就好啦

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值