How to tell the difference between an iframe loading vs. entire page loading in a UIWebView?

陌路散爱 提交于 2019-12-02 23:18:38
 (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{

BOOL isFrame = ![[[request URL] absoluteString] isEqualToString:[[request mainDocumentURL] absoluteString]];

}

I just used this method:

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.

I've successfully used the HTTP "referer" (the actual header is misspelled) field to detect this. This should either be the URL of the main page (available from the webview) or something else -- probably a frame.

NSString *referer = [request.allHTTPHeaderFields objectForKey:@"Referer"];
NSString *currentPage = [webView.request.mainDocumentURL absoluteString];
BOOL isFrameLoad = [referer isEqualToString:currentPage] == NO;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!