如何在 iOS 中一起加载 webview 中的两个 url?
How to load two urls in webview Together in iOS?
我正在网络视图中加载 url。
我有两种类型的数据要显示
1)文本(html 字符串)
2)url
这是我的代码
To display text
[self.webView loadHTMLString:[self.arr objectAtIndex:indexPath.row][@"text"] baseURL:nil];
To display url
NSString *str = [NSString stringWithFormat:@"%@%@",URL,strpdf];
NSURL *websiteUrl = [NSURL URLWithString:str];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:websiteUrl];
[self.webView loadRequest:urlRequest];
问题是当我加载两个数据时,数据被覆盖。
我想在 webview 中显示这两个数据
但现在只有文字或图片显示。
有什么解决办法。
我的演示如下:
#import "ViewController.h"
@interface ViewController ()<UIWebViewDelegate>
@property(nonatomic, strong) UIWebView *webView;
@property (nonatomic, copy) NSString *html_str;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initUI ];
}
- (void)initUI {
_html_str = @"I love google..."; // the html text you want
CGRect frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
_webView = [[UIWebView alloc] initWithFrame:frame];
_webView.delegate = self;
NSString *str = [NSString stringWithFormat:@"http://chsezhsan195030.e-fae.cn/"];
NSURL *websiteUrl = [NSURL URLWithString:str];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:websiteUrl];
[self.webView loadRequest:urlRequest];
[self.view addSubview:self.webView];
}
#pragma mark - webview delegate
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *script_str = [NSString stringWithFormat:@"var body = document.getElementsByTagName('body')[0];\
var script = document.createElement('div');\
script.innerText = '12345678'%@;\
script.style.width = window.innerWidth + 'px';\
script.style.height = '300px';\
script.style.backgroundColor = '#eeeeee';\
body.appendChild(script); \
body.insertBefore(script, body.childNodes[0]);",_html_str];
NSString *function = [NSString stringWithFormat: @"var script = document.createElement('script');\
script.type = 'text/javascript';\
script.text = \'function myFunction() { \
%@ };'\
document.getElementsByTagName('head')[0].appendChild(script);", script_str];
[self.webView stringByEvaluatingJavaScriptFromString:function];
[self.webView stringByEvaluatingJavaScriptFromString:@"myFunction()"];
}
@end
但是你要注意大部分url是为了注入js代码而被阻止的,特别是https
url,所以你不能插入上面的html文本google 的索引页。
我正在网络视图中加载 url。 我有两种类型的数据要显示
1)文本(html 字符串)
2)url
这是我的代码
To display text
[self.webView loadHTMLString:[self.arr objectAtIndex:indexPath.row][@"text"] baseURL:nil];
To display url
NSString *str = [NSString stringWithFormat:@"%@%@",URL,strpdf];
NSURL *websiteUrl = [NSURL URLWithString:str];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:websiteUrl];
[self.webView loadRequest:urlRequest];
问题是当我加载两个数据时,数据被覆盖。 我想在 webview 中显示这两个数据 但现在只有文字或图片显示。
有什么解决办法。
我的演示如下:
#import "ViewController.h"
@interface ViewController ()<UIWebViewDelegate>
@property(nonatomic, strong) UIWebView *webView;
@property (nonatomic, copy) NSString *html_str;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initUI ];
}
- (void)initUI {
_html_str = @"I love google..."; // the html text you want
CGRect frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
_webView = [[UIWebView alloc] initWithFrame:frame];
_webView.delegate = self;
NSString *str = [NSString stringWithFormat:@"http://chsezhsan195030.e-fae.cn/"];
NSURL *websiteUrl = [NSURL URLWithString:str];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:websiteUrl];
[self.webView loadRequest:urlRequest];
[self.view addSubview:self.webView];
}
#pragma mark - webview delegate
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *script_str = [NSString stringWithFormat:@"var body = document.getElementsByTagName('body')[0];\
var script = document.createElement('div');\
script.innerText = '12345678'%@;\
script.style.width = window.innerWidth + 'px';\
script.style.height = '300px';\
script.style.backgroundColor = '#eeeeee';\
body.appendChild(script); \
body.insertBefore(script, body.childNodes[0]);",_html_str];
NSString *function = [NSString stringWithFormat: @"var script = document.createElement('script');\
script.type = 'text/javascript';\
script.text = \'function myFunction() { \
%@ };'\
document.getElementsByTagName('head')[0].appendChild(script);", script_str];
[self.webView stringByEvaluatingJavaScriptFromString:function];
[self.webView stringByEvaluatingJavaScriptFromString:@"myFunction()"];
}
@end
但是你要注意大部分url是为了注入js代码而被阻止的,特别是https
url,所以你不能插入上面的html文本google 的索引页。