ios自动更新(一)

来源:互联网 发布:convertfrom json 编辑:程序博客网 时间:2024/06/01 09:01

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

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


1. 判断是否是第一次启动app,如果是第一次启动,将资源文件copy到沙盒
    // 如果沙箱指定目录已存在,先删除他
    if([fileManagerfileExistsAtPath:[[selfclass]resourceRootPath]]) {
       if(![fileManagerremoveItemAtPath:[[selfclass]resourceRootPath]error:&error]) {
           DLog(@"remove resourceFolder error: %@", error.localizedDescription);
           returnNO;
        }
    }
   
    // 拷贝资源文件到沙箱指定目录
    NSString *bundlePath = [[NSBundlemainBundle]pathForResource:bundleNameofType:nil];
    if(![fileManagercopyItemAtPath:bundlePathtoPath:[[selfclass]resourceRootPath]error:&error]) {
       DLog(@"copy resource files error: %@", error.localizedDescription);
       returnNO;
    }

2. 初始化文件索引,将沙盒中的资源文件的索引添加到sqlite 数据库
3. 检查更新,将数据库中的版本号和服务器上的版本号进行对比,如果服务器上的版本号大,在沙盒中创建新版本的资源目录,并且开始下载服务器文件,更新数据库
        // 开始下载,文件名会自动去取url的文件名
           NSURLRequest *request = [NSURLRequestrequestWithURL:[NSURLURLWithString:versionRecord.downurl]cachePolicy:NSURLRequestReloadIgnoringCacheDatatimeoutInterval:60];
           AFDownloadRequestOperation *operation = [[AFDownloadRequestOperationalloc]initWithRequest:requesttargetPath:targetFullPathshouldResume:YES];
            operation.shouldOverwrite = YES;
            [operationsetCompletionBlockWithSuccess:^(AFHTTPRequestOperation * _Nonnull operation,id _Nonnull responseObject) {
               // 下载成功才更新索引数据库
                [[selfclass]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);
            }];
           
            [[NSOperationQueuemainQueue]addOperation:operation]; 


原创粉丝点击