美文网首页iOS开发
iOS 关于webView的一些小技巧记录

iOS 关于webView的一些小技巧记录

作者: 涂了摩丝丶 | 来源:发表于2017-05-31 15:56 被阅读40次

    1.webView图片自适应
    -(void)webViewDidFinishLoad:(UIWebView *)webView
    {
    NSString *script = [NSString stringWithFormat:
    @"var script = document.createElement('script');"
    "script.type = 'text/javascript';"
    "script.text = "function ResizeImages() { "
    "var img;"
    "var maxwidth=%f;"
    "for(i=0;i <document.images.length;i++){"
    "img = document.images[i];"
    "if(img.width > maxwidth){"
    "img.width = maxwidth;"
    "}"
    "}"
    "}";"
    "document.getElementsByTagName('head')[0].appendChild(script);", SCREEN_WIDTH - 20];
    [webView stringByEvaluatingJavaScriptFromString: script];
    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
    }

    2.webView根据内容自适应高度
    -(void)webViewDidFinishLoad:(UIWebView *)webView{
    //获取到webview的高度
    CGFloat height = [[self.webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
    self.webView.frame = CGRectMake(self.webView.frame.origin.x,self.webView.frame.origin.y, kScreenWidth-20, height+40);
    }

    3.webView网络图片添加点击事件
    -(void)webViewDidFinishLoad:(UIWebView *)webView
    {
    //这里是js,主要目的实现对url的获取
    static NSString * const jsGetImages =
    @"function getImages(){
    var objs = document.getElementsByTagName("img");
    var imgScr = '';
    for(var i=0;i<objs.length;i++){
    imgScr = imgScr + objs[i].src + '+';
    };
    return imgScr;
    };";

    [webView stringByEvaluatingJavaScriptFromString:jsGetImages];//注入js方法
    NSString *urlResurlt = [webView stringByEvaluatingJavaScriptFromString:@"getImages()"];
    

    NSString *urlResurlt = [webView stringByEvaluatingJavaScriptFromString:@"getImages()"];
    _webImgArr = [NSMutableArray arrayWithArray:[urlResurlt componentsSeparatedByString:@"+"]];
    if (_webImgArr.count >= 2) {
    [_webImgArr removeLastObject];
    }
    //urlResurlt 就是获取到得所有图片的url的拼接;_webImgArr就是所有Url的数组
    [_webView stringByEvaluatingJavaScriptFromString:@"function registerImageClickAction(){
    var imgs=document.getElementsByTagName('img');
    var length=imgs.length;
    for(var i=0;i<length;i++){
    img=imgs[i];
    img.onclick=function(){
    window.location.href='image-preview:'+this.src}
    }
    }"];

    [_webView stringByEvaluatingJavaScriptFromString:@"registerImageClickAction();"];
    [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitDiskImageCacheEnabled"];
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitOfflineWebApplicationCacheEnabled"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    

    //在这个方法中捕获到图片的点击事件和被点击图片的url

    • (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest )request navigationType:(UIWebViewNavigationType)navigationType {
      //预览图片
      if ([request.URL.scheme isEqualToString:@"image-preview"]) {
      NSString
      path = [request.URL.absoluteString substringFromIndex:[@"image-preview:" length]];
      path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
      if (_webImgArr.count != 0) {

          // 这里添加需要操作的事件  
        }
      
        return NO;
      

      }
      return YES;
      }

    相关文章

      网友评论

        本文标题:iOS 关于webView的一些小技巧记录

        本文链接:https://www.haomeiwen.com/subject/lxhzxxtx.html