ios,WebView,WKWebView

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;" //缩放系数
                       "for(var i=0;i 

留白

https://www.jianshu.com/p/daa53aa44700


webview中网址监听,和点击监听

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
  //点击事件
//    if ([request.mainDocumentURL.relativePath isEqualToString:@"xxxxxx"]) {
//        return false;  //执行本地代码,返回false不让网页读取网络资源
//
//    }

//网址监听
   //    //1.截取到当前地址
        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加载内容,否则不


}

wkWebView

查看WKNavigationDelegate注释
查看WKUIDelegate注释
获取title
WKWebView有个title属性,直接取出来就行了,用的是kvo属性监听。

_wkWebView.UIDelegate = self;
[_wkWebView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
//WkWebView的 回调
-(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加个判断 区分https还是http
https://www.jianshu.com/p/31d226a044dc

字体大小

- (void)webViewDidFinishLoad:(UIWebView*)webView {
    ///250是字体的大小,根据自己的需求修改
    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
// 显示一个按钮。点击后调用completionHandler回调
- (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];
}

// 显示两个按钮,通过completionHandler回调判断用户点击的确定还是取消按钮
- (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];
    
}

// 显示一个带有输入框和一个确定按钮的,通过completionHandler回调用户输入的内容
- (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];
}

你可能感兴趣的:(ios,WebView,WKWebView)