iOS 加载本地的html文件

时间:2023-03-08 20:20:55
iOS 加载本地的html文件

方法1:

self.webView  = [[UIWebView alloc]initWithFrame:self.view.bounds];

NSString *path = [[NSBundle mainBundle]pathForResource:@"index" ofType:@"html"];

NSString *html = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

[self.webView loadHTMLString:html baseURL:nil];

[self.view addSubview:self.webView];

方法2:

NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
NSString *html = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:html baseURL:baseURL];