iOS小技能:加载本地HTML、pdf、doc、excel文件 & HTML字符串与富文本互转

2,298 阅读2分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第21天,点击查看活动详情

前言

  • iOS加载本地HTML、pdf、doc、excel文件,可采用WebView或者UIDocumentInteractionController进行实现。
  • HTML字符串与富文本互转

应用场景:使用原生视图UILabel显示服务端返回的带有HTML标签的内容

I 加载本地HTML文件

当你在手机打开html文件的时候,是不是用以下这个方法

将它作为邮件的附件,在手机端选择其他应用打开,将html文件存储到文件的iCloud/本机 再根据文件名称打开即可

如果你有需求在手机端打开本地html的需求,又觉得使用其他方法麻烦或者不管用的时候,推荐你可以自己写个简单的app进行打开。

1.1 原理

使用[_webView loadHTMLString:html baseURL:baseURL]; 进行代码加载

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    
        [self setupWebViewController:  [[NSBundle mainBundle] pathForResource:KNUserGuideURL ofType:@"html"]  ];

    
//    [self setupAXWebViewController:  [[NSBundle mainBundle] pathForResource:KNUserGuideURL ofType:@"html"]  ];

}

- (void)setupWebViewController:(NSString*)path{
    
    
    
    
    NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
    NSString *html = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    
    [_webview loadHTMLString:html baseURL:baseURL];// 进行代码加载


    
}


1.2 源码

1、从CSDN下载源码地址:https://download.csdn.net/download/u011018979/15448928 2、private 仓库:github.com/zhangkn/NSA… 3、【private 仓库】github.com/zhangkn/loa… 相关文章:blog.csdn.net/z929118967/…

1.3 核心代码

  • 通过文件名获取path
                                        [wself setupAXWebViewController:  [[NSBundle mainBundle] pathForResource:KNUserGuideURL ofType:@"html"]  ];// 通过文件名获取path

  • 根据path进行代码的加载
- (void)setupAXWebViewController:(NSString*)path{
    
    
    
    
    NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
    NSString *html = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    
    
    AXWebViewController *webVC = [[AXWebViewController alloc] initWithHTMLString:html baseURL:baseURL];

    

    
    webVC.showsToolBar = NO;
    webVC.navigationController.navigationBar.translucent = NO;
    webVC.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.100f green:0.100f blue:0.100f alpha:0.800f];
    webVC.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.996f green:0.867f blue:0.522f alpha:1.00f];
    
    UINavigationController *tmp = [[UINavigationController alloc]initWithRootViewController:webVC];
    
    [self presentViewController:tmp animated:YES completion:^{
        
    } ];
}

II 加载pdf、doc、excel文件

2.1 使用WebView打开

  1. iOS12之前使用UIWebView

- (void)openpdfByWebView{
    

    NSString *path = [[NSBundle
                       mainBundle] pathForResource:@"ios.pdf"
                      ofType:nil];
    NSURL *url = [NSURL
                  fileURLWithPath:path];
    NSURLRequest *request = [NSURLRequest
                             requestWithURL:url];
    [self.webView
     loadRequest:request];

}



  1. iOS12之后采用WKWebView
- (void)openpdfByWKWebView{
    
    WKWebView *tmp = [WKWebView new] ;
    
    
    self.wk_webView =tmp;
    [self.view addSubview:self.wk_webView];
    
    self.wk_webView.frame = self.view.frame;
    

    NSString *path = [[NSBundle
                       mainBundle] pathForResource:@"ios.pdf"
                      ofType:nil];
    NSURL *url = [NSURL
                  fileURLWithPath:path];
    NSURLRequest *request = [NSURLRequest
                             requestWithURL:url];
    [self.wk_webView
     loadRequest:request];


}


2.2 使用文档控制器进行文件预览、分享、打印、存储到手机

kunnan.blog.csdn.net/article/det…

III HTML字符串与富文本互转

demo源码下载:download.csdn.net/download/u0…

3.1 html转换为富文本

    NSString *html = @"<p style='color:green'>博客<span style='color:#e83c36;'><a>https://kunnan.blog.csdn.net/<a/></span><br/>微信公众号:<span style='color:red'>iOS逆向</span><br/>“订阅”一次 ,享受终身服务的快乐。</br><span style='color:red'>专注《iOS应用逆向与安全》</span>(包括iOS基础)</p>";
    NSAttributedString *attStr = [[NSAttributedString alloc] initWithData:[html dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];



3.2 富文本转换为html

- (void)NSAttributedStringtohtmlWith:(NSAttributedString*)attStr{
    
    NSDictionary *dic = @{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:@(NSUnicodeStringEncoding)};
    
    
    NSData *data = [attStr dataFromRange:NSMakeRange(0, attStr.length) documentAttributes:dic error:nil];
    
    
    
    NSString *str = [[NSString alloc] initWithData:data encoding:NSUnicodeStringEncoding];
    
    NSLog(@"NSAttributedStringtohtmlWith:%@",str);
    
    [self gotoAXWebViewControllerWithHtmlstr:str];

    
    

}

see also

html进阶:1、【Meta 标签的 http-equiv 属性使用指南】content-Type(显示字符集的设定)、Refresh(刷新)2、用 iframe 解决跨域请求问题

blog.csdn.net/z929118967/…