UIWebView
webview加載url地址或更換url
代碼寫到哪里都行
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"xxxxx"]]];
webview中的圖片適配
寫在- (void)webViewDidFinishLoad:(UIWebView *)webView {代理里面
NSString *jsStr = [NSString stringWithFormat:
@"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.text = \"function ResizeImages() { "
"var myimg,oldwidth,oldheight;"
"var maxwidth=%f;" //縮放系數(shù)
"for(var i=0;i <document.images.length;i++){"
"myimg = document.images[i];"
"oldwidth = myimg.width;"
"oldheight = myimg.height;"
"myimg.style.width = maxwidth+'px';"
"myimg.style.height = (oldheight * (maxwidth/oldwidth))+'px';"
"}"
"}\";"
"document.getElementsByTagName('head')[0].appendChild(script);",SCREEN_WIDTH-20];
[webView stringByEvaluatingJavaScriptFromString:jsStr];
[webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
留白
http://www.reibang.com/p/daa53aa44700
webview中網(wǎng)址監(jiān)聽肛根,和點(diǎn)擊監(jiān)聽
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
//點(diǎn)擊事件
// if ([request.mainDocumentURL.relativePath isEqualToString:@"xxxxxx"]) {
// return false; //執(zhí)行本地代碼喧兄,返回false不讓網(wǎng)頁讀取網(wǎng)絡(luò)資源
//
// }
//網(wǎng)址監(jiān)聽
// //1.截取到當(dāng)前地址
NSString *url1=request.URL.absoluteString;
//2.做你想做的操作
if ([url1 isEqualToString:@"http://xxxxx"]) {
// WFXXXXXX *vc=[[XXXXXXX alloc]init];
// [self.navigationController pushViewController:vc animated:YES];
return NO;
}
return true; //為yes加載內(nèi)容狼荞,否則不
}
wkWebView
查看WKNavigationDelegate注釋
查看WKUIDelegate注釋
獲取title
WKWebView有個(gè)title屬性由蘑,直接取出來就行了闽寡,用的是kvo屬性監(jiān)聽代兵。
_wkWebView.UIDelegate = self;
[_wkWebView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
//WkWebView的 回調(diào)
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
if ([keyPath isEqualToString:@"title"]) {
if (object == self.wkWebView) {
self.title = self.wkWebView.title;
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
3
- (void)dealloc{
[_wkWebView removeObserver:self forKeyPath:@"title"];
}
支持https
把url加個(gè)判斷 區(qū)分https還是http
http://www.reibang.com/p/31d226a044dc
字體大小
- (void)webViewDidFinishLoad:(UIWebView*)webView {
///250是字體的大小,根據(jù)自己的需求修改
NSString* fontSize = [NSString stringWithFormat:@"%d",250];
fontSize = [fontSize stringByAppendingFormat:@"%@",@"%"];;
NSString* str = [NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%@'",fontSize];
[self.webView stringByEvaluatingJavaScriptFromString:str];
}
WebView不顯示JS的alert彈窗的解決辦法
換成WKWebView
WKWebView不顯示JS的alert彈窗的解決辦法
self.webView.UIDelegate = self;
#pragma mark -- WKUIDelegate
// 顯示一個(gè)按鈕。點(diǎn)擊后調(diào)用completionHandler回調(diào)
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:message message:nil preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
completionHandler();
}]];
[self presentViewController:alertController animated:YES completion:nil];
}
// 顯示兩個(gè)按鈕爷狈,通過completionHandler回調(diào)判斷用戶點(diǎn)擊的確定還是取消按鈕
- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL))completionHandler{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:message message:nil preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
completionHandler(YES);
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
completionHandler(NO);
}]];
[self presentViewController:alertController animated:YES completion:nil];
}
// 顯示一個(gè)帶有輸入框和一個(gè)確定按鈕的植影,通過completionHandler回調(diào)用戶輸入的內(nèi)容
- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * _Nullable))completionHandler{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:nil preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
}];
[alertController addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
completionHandler(alertController.textFields.lastObject.text);
}]];
[self presentViewController:alertController animated:YES completion:nil];
}