美文网首页iOS随记
iOS 版本更新

iOS 版本更新

作者: 脚踏实地的小C | 来源:发表于2017-09-25 18:01 被阅读11次

今天公司要给APP加上“检查更新”这个服务,因为在上一家公司做过,所以没花太多时间就完成了。不过还是看以前公司的代码完成的,所以觉得还是写下来比较好,以免以后自己给忘了。
  因为比较简单,所以下面直接上代码:

.h里面的代码

#import <Foundation/Foundation.h>

@interface VersionUpdateObject : NSObject

+(instancetype)sharedInstance;
- (void)updateApp;

@end

.m里面的代码

#import "VersionUpdateObject.h"
#import "SVProgressHUD.h"

#define kAPP_URL  @"http://itunes.apple.com/lookup?id="
#define kAppID  @"你自己的APPID"

@interface VersionUpdateObject ()
{
    NSTimer *timer;
}
@property(nonatomic,strong)NSString *trackViewUrl;//更新时用到的地址

@end
@implementation VersionUpdateObject

+(instancetype)sharedInstance
{
    static id _versionUpdate;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _versionUpdate = [[self alloc] init];
    });
    return _versionUpdate;
}
#pragma mark - 检查更新
- (void)updateApp
{
    [SVProgressHUD show];
    if (!timer) {
        timer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(SvProgressDismiss) userInfo:nil repeats:NO];
    }
    NSString *urlStr = [NSString stringWithFormat:@"%@%@", kAPP_URL, kAppID];
    NSURL *url = [NSURL URLWithString:urlStr];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    //网络请求
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSError *err;
        
        if (data.length == 0) {
            return;
        }
        NSDictionary *appInfoDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&err];

        [self SvProgressDismiss];
        if (err) {
            NSLog(@"%@", err.description);
            return;
        }
        NSArray *resultArray = [appInfoDict objectForKey:@"results"];
        if (![resultArray count]) {
            return;
        }
        
        NSDictionary *infoDict = [resultArray objectAtIndex:0];
        
        //获取服务器上应用的最新版本号
        NSString *updateVersion = infoDict[@"version"];
        NSString *newVersionStr = [updateVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
        //_trackViewUrl : 更新的时候用到的地址
        _trackViewUrl = infoDict[@"trackViewUrl"];
        
        //获取当前设备中应用的版本号
        NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
        NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
        NSString *oldVersionStr = [currentVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
        
//        oldVersionStr = @"1.2";
        //判断两个版本是否相同
        if ([oldVersionStr doubleValue] < [newVersionStr doubleValue]) {
            
            NSString *messageStr = @"发现新版本";
            
            dispatch_sync(dispatch_get_main_queue(), ^{
                
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:messageStr delegate:self cancelButtonTitle:@"暂不更新" otherButtonTitles:@"立即更新", nil];
                alert.tag = [kAppID intValue];
                [alert show];
            });
        }else {  //版本号和app store上的一致
            if (!self.isAutoShowStatus) {//非自动更新
                dispatch_sync(dispatch_get_main_queue(), ^{
                    
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"暂无新版本" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    alert.tag = [kAppID intValue] + 1;
                    [alert show];
                });
            }
        }
    }];
    [task resume];
}
//判断用户点击了哪一个按钮
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == [kAppID intValue])
    {
        if (buttonIndex == 1)
        { //点击”升级“按钮,就从打开app store上应用的详情页面
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.trackViewUrl]];
        }
    }
}

/**
 超过20秒。则取消旋转
 */
-(void)SvProgressDismiss
{
    NSLog(@"取消菊花旋转");
    [SVProgressHUD dismiss];
    [timer invalidate];
    timer = nil;
    
}

相关文章

  • iOS版本更新

    iOS APP版本 更新

  • iOS 上传AppStore 被拒汇总

    (1)、苹果要求版本更新必须使用iOS版本更新内置更新机制。 4. Design: Preamble Design...

  • iOS版本更新

    1、获取当前版本 NSString *current_version = [[[NSBundle mainBund...

  • iOS 版本更新

    获取本机版本号 NSDictionary *localDic = [[NSBundle mainBundle] i...

  • iOS版本更新

    版本更新 #pragma mark -检查版本更新 - (void)checkNewVersion { // NS...

  • iOS 版本更新

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

  • iOS 版本更新

    1.获取本地版本信息 2.获取App Store版本信息 3.新旧版本对比 代码: 请求的基类中获取App Sto...

  • iOS版本更新

    最近,应项目需求,简单写了一个版本更新的提示,有需要的码友可以去下载看看,实现起来比较简单。 效果图 首先是请求a...

  • iOS版本更新

    一、获取app线上版本苹果提供网址:https://itunes.apple.com/lookup网址所需参数:i...

  • iOS 版本更新

    今天公司要给APP加上“检查更新”这个服务,因为在上一家公司做过,所以没花太多时间就完成了。不过还是看以前公司的代...

网友评论

    本文标题:iOS 版本更新

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