美文网首页
iOS 版本更新

iOS 版本更新

作者: 何以_aaa | 来源:发表于2017-08-15 17:05 被阅读32次

    众所周知苹果拒绝任何带有版本更新的提示出现在页面中,因此我们通过接口获取,处理是否显示更新提示。

    1.判断版本号

    请求服务器版本号:

    NSString *newVersion = @"3.2.1";
    

    获取当前版本号:

    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    NSString *lastVersion = infoDic[@"CFBundleShortVersionString"];
    
    1.1 判断字符串
    if (![lastVersion isEqualToString:newVersion]) {
      //更新
    }else {
      //不更新
    }
    
    1.2 比较大小
    lastVersion = [lastVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
    if (lastVersion.length==2) {
        lastVersion  = [lastVersion stringByAppendingString:@"0"];
    }else if (lastVersion.length==1){
        lastVersion  = [lastVersion stringByAppendingString:@"00"];
    }
    newVersion = [newVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
    if (newVersion.length==2) {
        newVersion  = [newVersion stringByAppendingString:@"0"];
    }else if (newVersion.length==1){
        newVersion  = [newVersion stringByAppendingString:@"00"];
    }
    if([lastVersion floatValue] < [newVersion floatValue]) {
        //更新
    }else {
        //不更新
    }
    

    2.提示弹框

    - (void)showUpdateWithNewVersion:(NSString *)newVersion isForce:(int)inForce content:(NSString *)content urlStr:(NSString *)urlStr {
        //当前项目版本号
        NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
        NSString *lastVersion = infoDic[@"CFBundleShortVersionString"];
    
        NSString *str = [content stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
    
        if ([lastVersion isEqualToString:newVersion]) {
            
            if (inForce == 2) {
                //非强制更新
                UIAlertController *alert =[UIAlertController alertControllerWithTitle:@"发现新版本" message:str preferredStyle:UIAlertControllerStyleAlert];
                alert.messageLabel.textAlignment = NSTextAlignmentLeft;
                
                UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
                [alert addAction:cancel];
                UIAlertAction *sure = [UIAlertAction actionWithTitle:@"立即更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    
                    // 跳转appStore
                    NSURL *url = [NSURL URLWithString:urlStr];
                    [[UIApplication sharedApplication] openURL:url];
                }];
                [alert addAction:sure];
                
                [self presentViewController:alert animated:YES completion:nil];
                
            }else if (inForce == 1) {
                //强制更新
                UIAlertController *alert =[UIAlertController alertControllerWithTitle:@"发现新版本" message:str preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *sure = [UIAlertAction actionWithTitle:@"立即更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    
                    // 跳转appStore
                    NSURL *url = [NSURL URLWithString:urlStr];
                    [[UIApplication sharedApplication] openURL:url];
                }];
                [alert addAction:sure];
                alert.messageLabel.textAlignment = NSTextAlignmentLeft;
                
                [self presentViewController:alert animated:YES completion:nil];
            }
        }
    }
    

    3.关于跳转Url

    注意跳转链接:有可能会直接跳转到 itunes 而不是 AppStore,跳转到 itunes 的后果是 itunes 里面不是 更新按钮 而是 打开按钮,点击打开按钮又返回到了 app,即失去了更新的功能。

    // 会跳转到 itune
    NSURL *url1 = [NSURL URLWithString:@"itms://itunes.apple.com/cn/app/id1127249355?mt=8"];
    // 跳转 AppStore
    NSURL *url2 = [NSURL URLWithString:@"itms-apps://itunes.apple.com/cn/app/id1127249355?mt=8"];
    // 跳转 AppStore
    NSURL *url3 = [NSURL URLWithString:@"https://itunes.apple.com/cn/app/id1127249355?mt=8"];
    
    [[UIApplication sharedApplication] openURL:url];
    

    4.Url拼接的Apple ID 去哪找

    4.1

    itunes connect 》我的APP 》Apple ID

    Apple ID.png
    4.2 若是在是找不到APP ID Apple ,另外一种url也可实现

    http://itunes.apple.com/lookup?bundleId=项目的BundelIdentifier&country=cn
    例如:
    http://itunes.apple.com/lookup?bundleId=com.xxxx.yyyy&country=cn

    相关文章

      网友评论

          本文标题:iOS 版本更新

          本文链接:https://www.haomeiwen.com/subject/kchvrxtx.html