webView加載url時(shí),我們只需將字符串url轉(zhuǎn)換為NSMutableURLRequest的request,然后執(zhí)行
根據(jù)field修改設(shè)置value, field要嚴(yán)格遵守http協(xié)議
- (void)setValue:(nullable NSString *)value forHTTPHeaderField:(NSString *)field;
\\如下:
NSURL *url = [NSURL URLWithString:htmlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[self.request setValue:@"cbsxf" forHTTPHeaderField:@"client_identify"];
[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:5.0];
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
[self.webView loadRequest:self.request];
上面的知識(shí)想必大家都會(huì)的,接下來進(jìn)入正題,webView加載url只是前提,接下來我們在已加載的webView中操作點(diǎn)擊下一級(jí)時(shí),該如何處理那?
首先我們需要找到webView代理方法:
webview每次加載之前都會(huì)調(diào)用這個(gè)方法, 如果返回NO,代表不允許加載這個(gè)請(qǐng)求
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
然后通過判斷來決定執(zhí)行那部分的url,判斷方式需要您自定義,如果您整體執(zhí)行添加http頭,可跳過此步。
下面舉例:
- (BOOL)webView:(UIWebView *)awebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString * const kBaseUrl = @"http://www.test.cn/demo/";
if ([kBaseUrl localizedCaseInsensitiveContainsString:[[request URL] host]]) {
NSURL *url = [request URL];
//默認(rèn)的緩存策略,根據(jù)HTTP頭中的信息進(jìn)行緩存處理
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setValue:@"cbsxf" forHTTPHeaderField:@"client_identify"];
//重新加載request
[webView loadRequest:request];
}
return YES;
}
備注:localizedCaseInsensitiveContainsString 可查看我的另一篇文章
OC代碼小知識(shí)
看似已經(jīng)完成,運(yùn)行項(xiàng)目后,會(huì)發(fā)現(xiàn)CPU暴漲為100%+。
查看原因,斷點(diǎn)調(diào)試,發(fā)現(xiàn)CPU暴漲原因,webView一直在重復(fù)執(zhí)行代理中的[webView loadRequest:request];
解決辦法:優(yōu)化loadRequest加載方式
//開始加載
- (BOOL)webView:(UIWebView *)awebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
//通過判斷是否已經(jīng)添加成功Http頭
BOOL headerIsPresent = [[request allHTTPHeaderFields] objectForKey:@"client_identify"] != nil;
if(headerIsPresent){
return YES;
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
NSURL *url = [request URL];
//默認(rèn)的緩存策略,根據(jù)HTTP頭中的信息進(jìn)行緩存處理
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setValue:@"cbsxf" forHTTPHeaderField:@"client_identify"];
[webView loadRequest:request];
});
});
return NO;
}
看樣子應(yīng)該是沒問題了,運(yùn)行項(xiàng)目,跳轉(zhuǎn)webview所在頁面(A),點(diǎn)擊webView中l(wèi)ink(B),觀察CPU,發(fā)現(xiàn)好了。
這時(shí)候我點(diǎn)擊了左上角的返回按鈕想重進(jìn)確認(rèn)下,結(jié)果發(fā)現(xiàn)頁面一直在循環(huán),B-->A-->B-->A--> ......-->B,無線循環(huán),根本跳轉(zhuǎn)不出來。
繼續(xù)斷點(diǎn)調(diào)試,發(fā)現(xiàn)返回后,先是執(zhí)行代理中的return NO,然后又去執(zhí)行的[self.webView loadRequest:request],反反復(fù)復(fù)。
解決辦法:監(jiān)聽我們執(zhí)行的返回操作,也就是[self.webView goBack];
//開始加載
- (BOOL)webView:(UIWebView *)awebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
//通過判斷是否已經(jīng)添加成功Http頭
BOOL headerIsPresent = [[request allHTTPHeaderFields] objectForKey:@"client_identify"] != nil;
//操作類型為前進(jìn)后退時(shí),正常去加載request
if (navigationType == UIWebViewNavigationTypeBackForward) {
return YES;
}
if(headerIsPresent){
return YES;
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
NSURL *url = [request URL];
//默認(rèn)的緩存策略,根據(jù)HTTP頭中的信息進(jìn)行緩存處理
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setValue:@"cbsxf" forHTTPHeaderField:@"client_identify"];
[webView loadRequest:request];
});
});
return NO;
}
運(yùn)行項(xiàng)目,測試無誤。
以上是本人在webView中點(diǎn)擊跳轉(zhuǎn)多級(jí)link并且需要添加http頭時(shí)處理辦法,如果您有更好的辦法,歡迎討論指正。
歡迎互相學(xué)習(xí)Github