avatar

WKWebView

加载本地html

1
2
3
4
5
6
7
8
9
10
11
12
13
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
WKWebView *wkWebView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:configuration];
wkWebView.backgroundColor = [UIColor whiteColor];
wkWebView.scrollView.showsVerticalScrollIndicator = NO;
[wkWebView.scrollView setContentInset:UIEdgeInsetsMake(0, 0, 44, 0)];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"abc" ofType:@"html"];
NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
NSError *error;
NSString *html = [NSString stringWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding
error:&error];
[wkWebView loadHTMLString:html baseURL:baseURL];
[self.view addSubview:wkWebView];

加载一个网页

1
2
3
4
5
6
7
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
WKWebView *wkWebView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:configuration];
wkWebView.backgroundColor = [UIColor whiteColor];
wkWebView.scrollView.showsVerticalScrollIndicator = NO;
NSURL *url = [[NSURL alloc]initWithString:@"http://www.baidu.com"];
[wkWebView loadRequest:[NSURLRequest requestWithURL:url]];
[self.view addSubview:wkWebView];

加载本地PDF

1
2
3
4
5
6
7
8
9
- (void)didLoadLocalPDF:(NSURL *)filePath
{
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
WKWebView *wkWebView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:configuration];
wkWebView.backgroundColor = [UIColor whiteColor];
wkWebView.scrollView.showsVerticalScrollIndicator = NO;
[wkWebView loadData:[NSData dataWithContentsOfURL:filePath] MIMEType:@"application/pdf" characterEncodingName:@"UTF-8" baseURL:nil];
[self.view addSubview:wkWebView];
}

加载在线PDF

1
2
3
4
5
6
7
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
WKWebView *wkWebView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:configuration];
wkWebView.backgroundColor = [UIColor whiteColor];
wkWebView.scrollView.showsVerticalScrollIndicator = NO;
NSURL *url = [[NSURL alloc]initWithString:@"https://xxx.pdf"];
[wkWebView loadRequest:[NSURLRequest requestWithURL:url]];
[self.view addSubview:wkWebView];
文章作者: 新风作浪
文章链接: http://duxinfeng.com/2017/04/10/WKWebView/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 新风作浪

评论