WKWebView加载html

Author Avatar
新风作浪 4月 10, 2017
  • 在其它设备中阅读本文章

加载本地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];