搜索
您的当前位置:首页正文

iOS-工程中添加HTML代码,SSZipArchive库的使用

来源:知库网
  • 一.使用.bundle存放HTML代码

  • 2、将拖到工程里
    工程列表.png
  • 3、上代码,web view加载HTML,此方法会加载中的index.html页面
-(void)createCustomUI
{
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
   
    _webView = [[UIWebView alloc] init];
    _webView.frame = CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-20);
    _webView.delegate = self;
    [self.view addSubview:_webView];

    NSString *bundleString = [[NSBundle mainBundle].resourcePath 
    NSBundle *bundle = [NSBundle bundleWithPath:bundleString];

    NSString *urlString = [bundle pathForResource:@"index" ofType:@"html"];
//加载webview
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
}
  • 二、使用SSZipArchive解压zip压缩包,获取HTML代码( )
  • 2、添加SSZipArchive第三方库,依赖于libz.tdb


    依赖库libz.tbd.png
@property (nonatomic, strong) UIWebView *webView; //webview
@property(copy , nonatomic)NSString *documentPath;//沙盒里documents文件夹路径
-(void)viewDidLoad {
      [super viewDidLoad];
      //加载页面
      [self createCustomUI];
      
      [self performSelectorInBackground:@selector(unzip) withObject:nil];
}
-(void)createCustomUI
{
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
   
    _webView = [[UIWebView alloc] init];
    _webView.frame = CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-20);
    _webView.delegate = self;
    [self.view addSubview:_webView];
}
//通过比较不同的版本号,来判断是否进行解压操作
-(void)unzip
{
    NSDictionary *dict = [[NSBundle mainBundle] infoDictionary];
    if ([dict[@"CFBundleShortVersionString"] isEqual:[[NSUserDefaults standardUserDefaults] objectForKey:@"VersionString"]]) {
//无操作
    }
    else {
         NSString *zipPath = [[NSBundle mainBundle]  ofType:@"zip"];
    NSString *destinationPath = self.documentPath;
    
    [SSZipArchive unzipFileAtPath:zipPath toDestination:destinationPath];
    //保存当前版本号
    [[NSUserDefaults standardUserDefaults] setObject:dict[@"CFBundleShortVersionString"] forKey:@"VersionString"];
    }
    //加载index.html
    [self performSelectorOnMainThread:@selector(showHtml) withObject:nil waitUntilDone:NO];
}
//懒加载,取得documents
-(NSString *)documentPath
{
    if (_documentPath == nil) {
        _documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    }
    return _documentPath;
}

-(void)showHtml
{
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[self.documentPath 
}
Top