![](/img/trans.png)
[英]iPhone - How can you get a URL from the Second UIViewController to Load in a WebView in the first UIViewController
[英]Two UIWebViews, how to set the second webview so when links are clicked it sends them to the first webview to load?
这个问题可能看起来有点奇怪/奇怪,但我有两个UIWebViews,其中一个设置为隐藏在.xib文件中,并且它的委托设置为我的视图控制器。 第二个UIWebView用于从隐藏的UIWebView中解析和“清理”html,并将其显示为“动员”页面给用户。 我的.xib文件中没有隐藏的UIWebView上没有委托。
我遇到的问题是,当第二个UIWebView完成加载时,用户点击第二个UIWebView内的链接,它完成加载未解析的网页,因为它没有发送此链接点击返回隐藏的UIWebView是加载,以便第二个UIWebView在完成加载时可以解析它。 我的两个UIWebView是我的.m文件中的属性。
有什么办法可以解决这个问题吗? 我感谢所提供的任何回复! 如果我没有明确说明我的问题,请告诉我!
一些代码:
。H:
#import <UIKit/UIKit.h>
@interface Web : UIViewController <UIWebViewDelegate>
@property (nonatomic, strong) NSURL *url;
@end
.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])];
}
您可以实现委托方法
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
这使您可以拦截加载请求并取消它们。 然后,您可以从请求中获取URL并将其移交给隐藏的Web视图。 唯一需要注意的是,IIRC,即使您使用-loadRequest:
加载自己的请求,也会调用此方法,因此您需要检查navigationType
参数。
你应该可以使用- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
。 将请求转发到第一个webView并返回NO
以防止第二个webView加载页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.