?????? 隨著H5的大火,手機端的頁面很多也被網(wǎng)頁端侵蝕了,作為手機端的開發(fā)人員,就算不需要學會H5的開發(fā),如何和h5(js)交互也成為了必要的技能,其中UIWebView的使用則是最最最基本的了,本文將從最基本處開始,直到與之交互,再收錄些許擴展,記錄一下這些知識以備后用。
1.代理方法
#pragma mark - webview代理方法
- (void)webViewDidStartLoad:(UIWebView *)webView
網(wǎng)頁開始加載,基本上不怎么用到,網(wǎng)頁加載過程中的一些事情可以在此時做;
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
這是與網(wǎng)頁交互的橋梁,如果想要與網(wǎng)頁互動,和H5協(xié)商好,不管是拼接URL,還是調(diào)用js,還是怎樣,在此方法都可實現(xiàn);
- (void)webViewDidFinishLoad:(UIWebView *)webView
頁面加載完成的操作,比如顯示網(wǎng)頁的標題等等
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
網(wǎng)頁加載失敗的方法,可以設(shè)置一個失敗的頁面,最好不要白的,提高用戶體驗。
這四個方法是一定會用到的。
2.初始化方法
我們經(jīng)常是需要給webview添加進度條和下拉刷新控件,
一般情況下,用一些很好的第三方的就完全夠用了,比如,進度條使用NJKWebViewProgress實現(xiàn),刷新嘛,當然是MJRefresh咯。
代碼如下:
在.h添加 UIWebViewDelegate,UIScrollViewDelegate,NJKWebViewProgressDelegate
@property (nonatomic,strong) NJKWebViewProgress *webViewProgress;
@property (nonatomic,strong) NJKWebViewProgressView *webViewProgressView;
在.m頁面初始化的時候?qū)崿F(xiàn)
_webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64 - 49)];
_webView.scrollView.mj_header =[MJRefreshNormalHeader headerWithRefreshingBlock:^{
[_webView reload];
}];
[_webView setBackgroundColor:[UIColor clearColor]];
[[_webView scrollView] setShowsVerticalScrollIndicator:NO];
[self.view addSubview:_webView];
[self setAutomaticallyAdjustsScrollViewInsets:NO];
_webViewProgress = [[NJKWebViewProgress alloc] init];
_webView.delegate = _webViewProgress;
_webView.scrollView.delegate= self;
_webViewProgress.webViewProxyDelegate = self;
_webViewProgress.progressDelegate = self;
CGRect navBounds = self.navigationController.navigationBar.bounds;
CGRect barFrame = CGRectMake(0, navBounds.size.height - 2, navBounds.size.width, 2);
_webViewProgressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];
_webViewProgressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
_webViewProgressView.backgroundColor = self.navigationController.navigationBar.backgroundColor;
[_webViewProgressView setProgress:0 animated:YES];
[self.navigationController.navigationBar addSubview:_webViewProgressView];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
#pragma mark - NJKWebViewProgressDelegate
-(void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress
{
???? [_webViewProgressView setProgress:progress animated:YES];
}
當然,如果頁面加載完要設(shè)置標題的話:
if (webView.isLoading) {
return;
} else {
[_webView.scrollView.mj_header endRefreshing];
_webviewTitle = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
[self.navigationItem setTitle:_webviewTitle];
}
頁面消失的時候需要:
[_webView stopLoading];
[_webViewProgressView setProgress:0 animated:YES];
這些基本的其實都可以一成不變了
3.webview失敗頁面處理
既然是網(wǎng)頁的加載,必然出現(xiàn)失敗的情況,失敗的處理也非常重要,但是很簡單,只需實現(xiàn)
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
????? //如果上一個請求還沒完,又新建了個請求會報錯
if(error.code == NSURLErrorCancelled )
{
return;
}
if (webView.request.URL.absoluteString && ![webView.request.URL.absoluteString isEqualToString:@""]) {
if (errorview) {
errorview.hidden = NO;
[webView addSubview:errorview];
}else{
errorview=[XFTools errorView:_webView.frame];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(kScreenWidth/2-75, kScreenHeight-250, 150, 80);
[btn setTitle:@"重新加載" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(webviewLoadUrl) forControlEvents:UIControlEventTouchUpInside];
[errorview addSubview:btn];
[webView addSubview:errorview];
}
}
}
4.擴展(WKWebView)
如今,iOS系統(tǒng)已經(jīng)升級到iOS10,使用iOS7及之前的系統(tǒng)的用戶比例已經(jīng)非常之小了,如果應用不需要支持他們,換言之就是從iOS8開始支持的話,不妨使用WKWebView,據(jù)說他有很多的優(yōu)勢,比如:60fps的滾動刷新率以及內(nèi)置手勢、將UIWebViewDelegate與UIWebView拆分成了14類與3個協(xié)議(查看官方文檔 )、與Safari相同的JavaScript引擎、占內(nèi)存更小等等。
用法:
別人的鏈接:點擊此處?
人家寫的非常好,就不去抄了。。。。
=====本文告一段落=====