iOS网页本地化以及更新

网页本地化以及更新

最近在做的项目中,有用到UIWebview加载网页的情况,问题呢,主要是,网页加载慢的话,就会出现大白页的情况,最后的处理方法是,将红包网页放到本地,在需要更新的时候,去下载网页zip,并且解压到沙盒下的Document中,下面我按照我项目的思路整理,不同场景可根据具体情况定。

思路整理如下

* 1.在登录之前,会调用配置信息的接口,默认的配置信息是从上次归档的文件中获取(第一次则是默认的配置信息),若是红包的版本有更新,则去网页指定路径下下载对应zip,到沙盒的某个路径下(Document中)
* 2.下载成功后,解压zip,到指定的路径下(Document),判断解压成功,删除掉zip,解压失败,下载或者解压失败,进行提示(异步下载,不影响其他过程)
* 3.在加载该网页的地方,先判断是否有解压好的html文件,有的话,使用本地加载方法,若是没有的话,还是使用,正常的网页加载
*

有一件大事的说哈,咱可是用了人家一个叫‘SSZipArchive’三方库,专门搞压缩,解压的

关键代码如下

》下面的方法是写在了NTArchiverData.m文件中,静态方法

************************************************
fileUrl:这个参数是你要下载zip的地址
fileName:这个参数是你下载好的zip的文件在某个路径下的名字
************************************************


/**
 *下载zip(如果存在这个路径下的zip,则直接获取,然则,下载去)
 */

+ (NSString*)DownloadTextFile:(NSString*)fileUrl fileName:(NSString*)fileName
{
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask,YES);//使用C函数NSSearchPathForDirectoriesInDomains来获得沙盒中目录的全路径。
    NSString *ourDocumentPath =[documentPaths objectAtIndex:0];
    NSString *FileName=[ourDocumentPath stringByAppendingPathComponent:fileName];//fileName就是保存文件的文件名
    NSFileManager *fileManager = [NSFileManager defaultManager];
    // Copy the database sql file from the resourcepath to the documentpath
    if ([fileManager fileExistsAtPath:FileName]){
        return FileName;
    }else
    {
        NSURL *url = [NSURL URLWithString:fileUrl];
        NSData *data = [NSData dataWithContentsOfURL:url];
        [data writeToFile:FileName atomically:YES];//将NSData类型对象data写入文件,文件名为FileName
    }
    return FileName;
}

/**
 *1.判断某个路径下的zip是否存在2.判断解压是否成功,并做相应的操作
 */
+ (void)isUnArchiveZipByFileName:(NSString *)fileName {
    //解压后如果遇到重复文件名会覆盖
    NSString *zipPath =[self DownloadTextFile:g_config.bonusUpdateUrl fileName:fileName];
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    NSString *destinationPath = documentsDirectory;
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    //判断压缩包是否存在
    if ([fileManager fileExistsAtPath:zipPath]) {
        //2.解压Zip
        //是否解压成功
        BOOL isArchive = [SSZipArchive unzipFileAtPath:zipPath toDestination:destinationPath overwrite:YES password:@"NatonBonus" error:nil];
        
        if (isArchive) {
            //在此解压成功的话,加载即可
            NTLog(@"红包本地化解压完成");
            //删除压缩包
            if ([[NSFileManager defaultManager] fileExistsAtPath:zipPath]) {
                [[NSFileManager defaultManager] removeItemAtPath:zipPath error:nil];
                NTLog(@"红包压缩包删除完成");
            }
        }else{
            
            [HUDNotificationCenter showMessage:@"红包资源解压失败,请重启" hideAfter:2.0f];
        }
    }
    else{
          [HUDNotificationCenter showMessage:@"红包压缩包,下载失败,请重启" hideAfter:2.0f];
    }
    
    
    
}

在调用配置信息接口,发现红包网页需要更新的时候调用如下方法

一句代码搞定,升级

 //红包版本升级,操作在此完成(异步加载红包zip)
        if ([[p objectForKey:@"version"] intValue] > [self.bonusVersion intValue]) {
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                [NTArchiverData isUnArchiveZipByFileName:@"Bonus.zip"];
            });
        }


加载网页的地方来啦

//抢红包

我的项目是需要给网页post参数的
我的zip(下载好的名字是Bonus.zip,自己定的)解压到Document下,解压后就删
除了,解压后的文件这个我是需要密码的,不过是压缩的人密码已经写死,我自然也
是写死的,解压后的路径是Document/Bonus/BonusOpen.html(我是让压缩包的
哥们直接把包名直接就写成了Bonus了,所以我知道解压后的包,文件夹肯定是Bonus,
里头放了他的一堆东西【html,css,fonts,img,js等,有用的就是html那个,
其余都是人家前端写的网页样式,与咱无关】




- (void)createWebWithHtmlStr:(NSString *)str andpara:(NSString *)params{
    
    NSString *postStr = [NSString stringWithFormat:@"token=%@&%@",g_loginUser.token,self.params];
    //判断如果本地化红包html是否下载完成
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentDirectory stringByAppendingPathComponent:@"/Bonus/BonusOpen.html"];
    NSMutableURLRequest *request;
    
    if ([fileManager fileExistsAtPath:filePath]) {
        NSURL *urlRequest = [NSURL URLWithString:[NSString stringWithFormat:@"?%@",postStr] relativeToURL:[NSURL URLWithString:filePath]];
        request = [[NSMutableURLRequest alloc] initWithURL:urlRequest
                                               cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:5.0f];
    }else {
       
        NSData *data = [postStr dataUsingEncoding:NSUTF8StringEncoding];
        request = [[NSMutableURLRequest alloc]initWithURL: [NSURL URLWithString:str]];
        [request setHTTPMethod: @"POST"];
        [request setHTTPBody: data];

    }
    
    UIWebView *web = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [web loadRequest:request];
    [self.view addSubview:web];
}

差不多啦,@**@!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,288评论 25 708
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,923评论 18 139
  • 1.导入<MediaPlayer/MediaPlayer.h>头文件 2.通过MPMoviePlayerViewC...
    ShenYj阅读 1,353评论 0 2
  • “如果我用你待我的方式来待你 .恐怕你早已离去” 好好体会这句话 !适合任何关系 !凡事换个角度 .假如你是我 ....
    迷失自我的妞儿阅读 412评论 0 0
  • 虽然每次失败我都归咎于运气不好,究竟为什么没有好运气,原因是我丢三落四粗心大意,这样运气也跟我失之交臂了
    无异阅读 117评论 0 0