ios自动更新(一)

apple 现在虽然不然热更新了,但是好多时候,我们会发现为了解决一点小问题就需要重新上线的尴尬,为了解决这个问题,分享服务器更新的思路,希望对大家有所帮助

首先我们应该创建版本更新管理类,主要的方法如下所示


1. 判断是否是第一次启动app,如果是第一次启动,将资源文件copy到沙盒
    // 如果沙箱指定目录已存在,先删除他
    if ([fileManager fileExistsAtPath :[[ self class ] resourceRootPath ]]) {
        if (![fileManager removeItemAtPath :[[ self class ] resourceRootPath ] error :&error]) {
            DLog ( @"remove resourceFolder error: %@" , error. localizedDescription );
            return NO ;
        }
    }
   
    // 拷贝资源文件到沙箱指定目录
    NSString *bundlePath = [[ NSBundle mainBundle ] pathForResource :bundleName ofType : nil ];
    if (![fileManager copyItemAtPath :bundlePath toPath :[[ self class ] resourceRootPath ] error :&error]) {
        DLog ( @"copy resource files error: %@" , error. localizedDescription );
        return NO ;
    }

2. 初始化文件索引,将沙盒中的资源文件的索引添加到sqlite 数据库
3. 检查更新,将数据库中的版本号和服务器上的版本号进行对比,如果服务器上的版本号大,在沙盒中创建新版本的资源目录,并且开始下载服务器文件,更新数据库
         // 开始下载,文件名会自动去取url的文件名
            NSURLRequest *request = [ NSURLRequest requestWithURL :[ NSURL URLWithString :versionRecord. downurl ] cachePolicy : NSURLRequestReloadIgnoringCacheData timeoutInterval : 60 ];
            AFDownloadRequestOperation *operation = [[ AFDownloadRequestOperation alloc ] initWithRequest :request targetPath :targetFullPath shouldResume : YES ];
            operation. shouldOverwrite = YES ;
            [operation setCompletionBlockWithSuccess :^( AFHTTPRequestOperation * _Nonnull operation, id   _Nonnull responseObject) {
                // 下载成功才更新索引数据库
                [[ self class ] updateIndexDB :versionRecord];
               
                DLog ( @"update file \"%@/%@\" from version %ld to version %ld" ,versionRecord. filepath , versionRecord. filename , version, versionRecord. versionno );
            } failure :^( AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
                DLog ( @"download resource(%@) error: %@" ,  operation. request . URL . absoluteString , error. localizedDescription );
            }];
           
            [[ NSOperationQueue mainQueue ] addOperation :operation];  


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值