
圖片.png
/**
WKWebView Demo
*/
- (void) wkWebViewDemo{
CGRect screen = [[UIScreen mainScreen] bounds];
//按鈕欄
CGFloat buttonBarWidth = 316;
UIView *buttonBar = [[UIView alloc] initWithFrame:CGRectMake((screen.size.width - buttonBarWidth)/2, 20, buttonBarWidth, 100)];
buttonBar.backgroundColor = [UIColor orangeColor];
[self.view addSubview:buttonBar];
//添加LoadHTMLString按鈕
UIButton *buttonLoadHTMLString = [UIButton buttonWithType:UIButtonTypeSystem];
[buttonLoadHTMLString setTitle:@"Load\nHTML\nString" forState:UIControlStateNormal];
buttonLoadHTMLString.titleLabel.numberOfLines = 3;
// buttonLoadHTMLString.backgroundColor = [UIColor grayColor];
// buttonLoadHTMLString.clipsToBounds = YES;
// buttonLoadHTMLString.layer.cornerRadius = 30;
buttonLoadHTMLString.frame = CGRectMake(0, 20, 117, 60);
[buttonLoadHTMLString addTarget:self action:@selector(testLoadHTMLString:) forControlEvents:UIControlEventTouchUpInside];
[buttonBar addSubview:buttonLoadHTMLString];
//添加LoadData按鈕
UIButton *buttonLoadData = [UIButton buttonWithType:UIButtonTypeSystem];
[buttonLoadData setTitle:@"Load\nData" forState:UIControlStateNormal];
buttonLoadData.titleLabel.numberOfLines = 2;
buttonLoadData.frame = CGRectMake(137, 20, 67, 60);
[buttonLoadData addTarget:self action:@selector(testLoadData:) forControlEvents:UIControlEventTouchUpInside];
[buttonBar addSubview:buttonLoadData];
//添加LoadRequest按鈕
UIButton *buttonLoadRequest = [UIButton buttonWithType:UIButtonTypeSystem];
[buttonLoadRequest setTitle:@"Load\nRequest" forState:UIControlStateNormal];
buttonLoadRequest.titleLabel.numberOfLines = 2;
buttonLoadRequest.frame = CGRectMake(224, 20, 92, 60);
[buttonLoadRequest addTarget:self action:@selector(testLoadRequest:) forControlEvents:UIControlEventTouchUpInside];
[buttonBar addSubview:buttonLoadRequest];
//添加WKWebView
self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 130, screen.size.width, screen.size.height-150)];
// self.webView.backgroundColor = [UIColor greenColor];
[self.view addSubview:self.webView];
}
- (void)testLoadHTMLString:(id)sender{
NSLog(@"%s", __func__);
NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
NSURL *bundleUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
NSError *error = nil;
NSString *html = [[NSString alloc] initWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:&error];
if (error == nil) {
[self.webView loadHTMLString:html baseURL:bundleUrl];
}else{
NSLog(@"加載失敗 error: %@", error.description);
}
}
- (void)testLoadData:(id)sender{
NSLog(@"%s", __func__);
NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
NSURL *bundleUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
NSData *htmlData = [[NSData alloc] initWithContentsOfFile: htmlPath];
[self.webView loadData:htmlData MIMEType:@"text/html" characterEncodingName:@"UTF-8" baseURL:bundleUrl];
}
- (void)testLoadRequest:(id)sender{
NSLog(@"%s", __func__);
NSURL * url = [NSURL URLWithString: @"https://guides.cocoapods.org"];
NSURLRequest * request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
self.webView.navigationDelegate = self;
NSLog(@"%s", __func__);
}
PS:記得@interface ViewController ()<WKNavigationDelegate>
#pragma mark - WKNavigationDelegate
//開(kāi)始加載時(shí)調(diào)用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
NSLog(@"開(kāi)始加載");
}
//內(nèi)容開(kāi)始返回時(shí)調(diào)用
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
NSLog(@"內(nèi)容開(kāi)始返回");
}
//加載完成之后調(diào)用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
NSLog(@"加載完成");
}
//加載失敗時(shí)調(diào)用
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error{
NSLog(@"加載失敗 error: %@", error.description);
}
上述代碼中使用的index.html,自己隨便寫(xiě)個(gè),也可以從網(wǎng)絡(luò)上載一個(gè)網(wǎng)頁(yè)。還是不過(guò)貼上一段簡(jiǎn)單的
<HEAD>
<meta name="viewport" content="width=device-width" />
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
</HEAD>
<BODY>
網(wǎng)頁(yè)內(nèi)容
</BODY>