简体   繁体   English

两个UIWebViews,如何设置第二个webview,以便在单击链接时将它们发送到第一个要加载的webview?

[英]Two UIWebViews, how to set the second webview so when links are clicked it sends them to the first webview to load?

This question may seem a little odd/weird, but I have two UIWebViews, one of which is set to hidden in the .xib file, and it's delegate set to my view controller. 这个问题可能看起来有点奇怪/奇怪,但我有两个UIWebViews,其中一个设置为隐藏在.xib文件中,并且它的委托设置为我的视图控制器。 The 2nd UIWebView is used to parse and "cleanup" the html from the hidden UIWebView and display it as a "mobilized" page to the user. 第二个UIWebView用于从隐藏的UIWebView中解析和“清理”html,并将其显示为“动员”页面给用户。 There is no delegate on the non-hidden UIWebView in my .xib file. 我的.xib文件中没有隐藏的UIWebView上没有委托。

The problem I'm having is that when the 2nd UIWebView finishes loading, the user taps on a link within the 2nd UIWebView, and it finishes loading the non parsed webpage because it didn't send this link click back to the hidden UIWebView to be loaded so that the 2nd UIWebView can parse it when it finishes loading. 我遇到的问题是,当第二个UIWebView完成加载时,用户点击第二个UIWebView内的链接,它完成加载未解析的网页,因为它没有发送此链接点击返回隐藏的UIWebView是加载,以便第二个UIWebView在完成加载时可以解析它。 My two UIWebView's are properties in my .m file. 我的两个UIWebView是我的.m文件中的属性。

Is there any way I can solve this? 有什么办法可以解决这个问题吗? I appreciate any responses offered! 我感谢所提供的任何回复! If i haven't stated my question clearly, please let me know! 如果我没有明确说明我的问题,请告诉我!

Some code: 一些代码:

.h: 。H:

#import <UIKit/UIKit.h>

@interface Web : UIViewController <UIWebViewDelegate>

@property (nonatomic, strong) NSURL *url;

@end

.m: .M:

#import "Web.h"

@interface  Web()

@property (nonatomic, strong) IBOutlet UIWebView *webView2;
@property (nonatomic, strong) IBOutlet UIWebView *webView;
@property (nonatomic, strong) NSMutableString *output;
@property (nonatomic, strong) NSString *wrapper;

@end

@implementation Web

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{   
    if(webView == self.webView2)
    {
        [self.webView loadRequest:(NSURLRequest*)request];
    }
    else
    {
        [self.webView loadRequest:(NSURLRequest*)request];
    }
    return NO;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.webView loadRequest:[NSURLRequest requestWithURL:self.url]];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{   
    [self cleanUp];

    [self.webView2 loadHTMLString:self.output baseURL:nil];
}

- (void)cleanUp
{
    NSString *input = [self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"ctl_ctl_cphBody_cphCenter_divHtml\").innerHTML;"];

    NSString *breadcrumbString = [self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('breadcrumb')[0].outerHTML;"];

    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        self.wrapper = @"<html><meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\"/><meta name=\"viewport\" content=\"initial-scale = 1.37,maximum-scale = 2.0\"/></html>";
    }
    else
    {
       self.wrapper = @"<html><meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\"/><meta name=\"viewport\" content=\"initial-scale = 0.57,maximum-scale = 2.0\"/></html>";
    }

    self.output = [NSMutableString stringWithString:self.wrapper];
    [self.output insertString:input atIndex:147];

    [self.output replaceOccurrencesOfString:breadcrumbString withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [self.output length])];
}

You can implement the delegate method 您可以实现委托方法

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

This lets you intercept load requests and cancel them. 这使您可以拦截加载请求并取消它们。 You can then grab the URL from the request and hand it off to your hidden webview. 然后,您可以从请求中获取URL并将其移交给隐藏的Web视图。 The only caveat is, IIRC, this method will be called even for requests that you load yourself with -loadRequest: , so you'll want to check the navigationType argument. 唯一需要注意的是,IIRC,即使您使用-loadRequest:加载自己的请求,也会调用此方法,因此您需要检查navigationType参数。

You should be able to use - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType . 你应该可以使用- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType Forward the request to the first webView and return NO to prevent the second webView from loading the page. 将请求转发到第一个webView并返回NO以防止第二个webView加载页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM