UIWebView源代码

时间:2016-06-01 17:50:17

标签: ios objective-c uiwebview

我在一个按钮上有一个 IBAction ,其中包含以下代码,我试图用它来检索UIWebView的源代码:

static class ContentViewHolder extends RecyclerView.ViewHolder {

    ImageView imageViewFileIcon;
    ImageView imageViewFileLockIcon;
    TextView textViewFileName;
    TextView textViewFileInfo;
    ImageView imageViewFavoriteIcon;
    ImageView imageViewInfoIcon;
    View infoButton;

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    public ContentViewHolder(View itemView) {
        super(itemView);

        imageViewFileIcon = (ImageView) itemView.findViewById(R.id.file_icon);
        imageViewFileLockIcon = (ImageView) itemView.findViewById(R.id.file_lock_icon);
        textViewFileName = (TextView) itemView.findViewById(R.id.file_name);
        textViewFileInfo = (TextView) itemView.findViewById(R.id.file_info);
        imageViewFavoriteIcon = (ImageView) itemView.findViewById(R.id.favorite_icon);
        imageViewInfoIcon = (ImageView) itemView.findViewById(R.id.info_icon);
        infoButton = itemView.findViewById(R.id.info_button);


        if (Utils.isJellyBeanMR1() && textViewFileName != null && textViewFileInfo != null) {
            // instead of creating a different layout for v17 we set alignment in the code:
            if (textViewFileName.getGravity() != Gravity.CENTER) {
                textViewFileName.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
            }
            textViewFileName.setTextDirection(View.TEXT_DIRECTION_LTR);
            textViewFileInfo.setTextDirection(View.TEXT_DIRECTION_LOCALE);
        }
    }
}

上述代码的使用来自我在此链接中提到的参考:Getting the HTML source code of a loaded UIWebView

但是,NSLog函数始终返回以下内容:

- (IBAction)loadInAWebView:(id)sender {
   [self.webView loadHTMLString:[NSString stringWithFormat:@"Hello: %@", _name.text] baseURL:nil];

   NSString *yourHTMLSourceCodeString = [_webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.innerHTML"];

   NSLog(@"%@", yourHTMLSourceCodeString);
}

这显然不能反映我认为应该是UIWebView的实际源代码:

<head></head><body></body>

实际上,UIWebView返回&#34; Hello,名称&#34;内容但相关的源代码没有。

如何编辑上面的代码以检索真正的UIWebView源代码?

1 个答案:

答案 0 :(得分:1)

您需要实施UIWebViewDelegate方法
- (void)webViewDidFinishLoad:(UIWebView *)webView并设置self.webView.delegate = self;
将代码NSString *yourHTMLSourceCodeString = [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.innerHTML"];放入此方法