如何判断UIWebView中iframe加载与整个页面加载的区别? [英] How to tell the difference between an iframe loading vs. entire page loading in a UIWebView?

查看:138
本文介绍了如何判断UIWebView中iframe加载与整个页面加载的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在[webView:shouldStartLoadWithRequest:navigationType:]事件中,如何判断加载内容的iframe与整个页面加载内容之间的区别?看起来两者都用相同的参数调用同一个事件,唯一的区别是要加载的URL,所以没有办法分辨两者之间的区别。

In the [webView:shouldStartLoadWithRequest:navigationType:] event, how can you tell the difference between an iframe that's loading content vs. the entire page loading content? It seems like the both call this same event with the same arguments, the only difference being the URL that's going to be loaded, so there is no way to tell the difference between the two.

我想到了几种方法来区分它们:

I've thought of a few ways to tell the difference though:


  • 解析页面的源代码并存储一个所有iframe src属性的列表,假设如果url加载是我列表中的其中一个,则它是iframe。当然,如果实际导航到页面,这个假设可能是不正确的。

  • 与上面相同,但在页面上运行一些JavaScript并获取iframe src属性。

  • 使用JavaScript,在用户尝试导航到页面时设置挂钩,类似于 http://niw.at/articles/2009/02/06/how-to-enable-the-popup-window-on -uiwebview / en

  • 使用JavaScript,在iframe尝试加载数据之前设置挂钩(不知道我会怎么做这样的事情)。

  • 修改每个iframe的src属性,在其src属性的末尾包含一个特殊字符串,例如#iframe的加载。使用此修改后的源代码加载UIWebView。虽然这只适用于第一次查询的原始页面上存在的iframe,而不是那些动态加载的iframe,例如通过JavaScript。

  • 通过在[webView:shouldStartLoadWithRequest:navigationType:]事件中返回 YES 来正常加载页面,并且一旦它出现完成加载,看看WebView的URL是否改变了...如果这样做意味着它是一个页面重定向,如果不是它可能意味着它是一个加载的iframe。

  • 使用JavaScript ,覆盖window.location属性setter以运行我自己的代码,然后才能实际更改窗口的位置。然后我可以与Objective-C进行通信,让它知道下一个加载实际上是一个重定向。这是在JavaScript中重定向的唯一方法吗?

  • Parse the source code of the page and store a list of all the iframe src attributes, assume that if the url loading is one of those in my list it's an iframe. Of course this assumption might be incorrect if it actually does navigate to the page.
  • Same as above, but run some JavaScript on the page and to get the iframe src attributes.
  • Using JavaScript, set up hooks for whenever a user tries to navigate to a page, similar to http://niw.at/articles/2009/02/06/how-to-enable-the-popup-window-on-uiwebview/en
  • Using JavaScript, set up hooks right before an iframe tries loading data (not sure how I would do something like this).
  • Modify each iframe's src attribute to include a special string at the end of it's src attribute e.g. "#iframe-loading". Load the UIWebView with this modified source code. Although this will only work for iframes that existed on the original page as it was queried the first time, not those that are loaded dynamically, e.g. via JavaScript.
  • Let the page load normally by returning YES in the [webView:shouldStartLoadWithRequest:navigationType:] event, and once it's done loading, see if the WebView's URL changed or not... if it did that means it was a page redirect if not it probably means it was an iframe that was loaded.
  • Using JavaScript, override the window.location property setter to run my own code before it will actually change the window's location. I could then communicate with Objective-C to let it know that the next load is actually going to be a redirect. Is this the only way to redirect in JavaScript though?

最好的方法是什么?你能想到我没有想到的任何方法吗? iframes有一个特殊的事件/属性,我可以用JavaScript操作来帮助我吗?

What's the best way to do this? Can you think of any ways I haven't thought of? Do iframes have a special event/property I could manipulate with JavaScript to help me out?

谢谢

更新:这并不像检查navigationType那么简单。虽然单击链接打开新页面将显示为navigationType = 0,但JavaScript重定向(更改window.location)将显示为navigationType = 5. iframe加载也显示为navigationType = 5.因此,当navigationType = 5,您不知道整个页面是通过JavaScript更改了URL还是只是在同一页面上加载了iframe。

Update: It's not as simple as just checking the navigationType. Although clicking a link to open up a new page will show up as navigationType = 0, a JavaScript redirect (changing window.location) will show up as navigationType = 5. An iframe load also shows up as navigationType = 5. So when navigationType = 5, you don't know if the entire page changed URLs via JavaScript or if it is simply an iframe loading on the same page.

推荐答案

我刚刚使用了这个方法:

I just used this method:


通过在[webView:shouldStartLoadWithRequest:navigationType:]事件中返回YES来正常加载页面,一旦完成加载,看看WebView的URL是否发生了变化......如果这样做意味着它是一个页面重定向,如果不是它可能意味着它是一个加载的iframe。

Let the page load normally by returning YES in the [webView:shouldStartLoadWithRequest:navigationType:] event, and once it's done loading, see if the WebView's URL changed or not... if it did that means it was a page redirect if not it probably means it was an iframe that was loaded.

这篇关于如何判断UIWebView中iframe加载与整个页面加载的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆