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

后端 未结 3 795
暖寄归人
暖寄归人 2021-02-04 09:18

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?

相关标签:
3条回答
  • 2021-02-04 09:46
     (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
    {
    
    BOOL isFrame = ![[[request URL] absoluteString] isEqualToString:[[request mainDocumentURL] absoluteString]];
    
    }
    
    0 讨论(0)
  • 2021-02-04 10:01

    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.

    0 讨论(0)
  • 2021-02-04 10:01

    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;
    
    0 讨论(0)
提交回复
热议问题